Merge pull request #299 from CarterPerez-dev/project/zingela
Project/zingela
This commit is contained in:
commit
a0407cdba2
|
|
@ -0,0 +1,4 @@
|
||||||
|
# ©AngelaMos | 2026
|
||||||
|
# .npmrc
|
||||||
|
strict-dep-builds=false
|
||||||
|
auto-install-peers=true
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
# ©AngelaMos | 2026
|
||||||
|
# .gitignore
|
||||||
|
|
||||||
|
# build artifacts
|
||||||
|
zig-out/
|
||||||
|
.zig-cache/
|
||||||
|
|
||||||
|
# private dev + research material (never public)
|
||||||
|
docs/
|
||||||
|
zig/
|
||||||
|
|
@ -0,0 +1,661 @@
|
||||||
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU Affero General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
our General Public Licenses are intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users.
|
||||||
|
|
||||||
|
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
|
||||||
|
them 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.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing under
|
||||||
|
this license.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey 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;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If 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 convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your version
|
||||||
|
supports such interaction) an opportunity to receive the Corresponding
|
||||||
|
Source of your version by providing access to the Corresponding Source
|
||||||
|
from a network server at no charge, through some standard or customary
|
||||||
|
means of facilitating copying of software. This Corresponding Source
|
||||||
|
shall include the Corresponding Source for any work covered by version 3
|
||||||
|
of the GNU General Public License that is incorporated pursuant to the
|
||||||
|
following paragraph.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU Affero 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 that a certain numbered version of the GNU Affero General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU Affero General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU Affero General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
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.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
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
|
||||||
|
state 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 Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for the
|
||||||
|
specific requirements.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
|
|
@ -0,0 +1,176 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- README.md -->
|
||||||
|
|
||||||
|
```
|
||||||
|
________ _ _ ____ _____ _ _
|
||||||
|
|__ /_ _| \ | |/ ___| ____| | / \
|
||||||
|
/ / | || \| | | _| _| | | / _ \
|
||||||
|
/ /_ | || |\ | |_| | |___| |___ / ___ \
|
||||||
|
/____|___|_| \_|\____|_____|_____/_/ \_\
|
||||||
|
```
|
||||||
|
|
||||||
|
[](https://github.com/CarterPerez-dev/Cybersecurity-Projects/tree/main/PROJECTS/advanced/zig-stateless-scanner)
|
||||||
|
[](https://ziglang.org)
|
||||||
|
[](https://www.kernel.org/doc/html/latest/networking/af_xdp.html)
|
||||||
|
[](https://musl.libc.org)
|
||||||
|
[](https://www.gnu.org/licenses/agpl-3.0)
|
||||||
|
|
||||||
|
> A stateless, line-rate mass TCP/UDP port scanner in the lineage of masscan and zmap. The name is Zulu for "to hunt." It holds no per-connection state, encodes probe identity in a SipHash cookie, walks the address space with a cyclic-group permutation, and ships as a single static binary with no libpcap, no libgmp, and no PCRE. It proves its packet logic against known-answer tests and stays memory-safe under Zig's ReleaseSafe.
|
||||||
|
|
||||||
|
## Why a stateless scanner in Zig
|
||||||
|
|
||||||
|
A normal TCP client tracks every connection: a socket, a state machine, a retransmit timer. That is fine for thousands of connections and impossible for billions. To sweep the routable IPv4 space you cannot keep a table, so the state moves into the packet itself. zingela encodes each probe's identity in the TCP sequence number with a keyed hash, and validates a reply with one hash recompute. No table, no timers, constant memory, and the transmit and receive engines share nothing and run flat out.
|
||||||
|
|
||||||
|
That makes it a sharp showcase for Zig: `extern struct` wire formats with compile-time size assertions, an RFC 1071 checksum in both scalar and `@Vector` form checked against the published vector, raw `AF_PACKET` and `AF_XDP` reached straight through `std.os.linux`, and every checksum and cookie pinned to a known-answer test. The result is a scanner that is fast where it can be, honest where it cannot, and safe by construction.
|
||||||
|
|
||||||
|
## The honest positioning
|
||||||
|
|
||||||
|
Read this before you believe any speed claim, from us or anyone else.
|
||||||
|
|
||||||
|
On stock Linux, masscan, zmap, and zingela all hit the same wall: the kernel `AF_PACKET` transmit path tops out around 1.5 to 2.5 million packets per second on a single core. Every one of these tools meets that ceiling. There is no raw-throughput number to win on identical hardware, and zingela does not claim one.
|
||||||
|
|
||||||
|
The only road past that ceiling in masscan is proprietary PF_RING ZC: a paid license, an out-of-tree kernel module, and specific NICs. Its headline figures, roughly 10 Mpps on a single 10GbE NIC and higher in dual-NIC demonstrations, are all PF_RING, never a stock kernel. zingela's road is `AF_XDP`, mainline in Linux since kernel 4.18, with no license and no proprietary module. That backend ships today behind the `-Dxdp` flag (pure syscalls, no libxdp). The head-to-head 10GbE benchmark against masscan is future work, and this README will not quote a line rate that has not been measured on real hardware.
|
||||||
|
|
||||||
|
The defensible win is the combination: `AF_XDP` acceleration with no PF_RING paywall, memory-safe Zig, a single static binary, correctness masscan never cleanly solved (RST suppression, validated-ICMP classification, accurate dedup), and a modern colorful interface.
|
||||||
|
|
||||||
|
## What Works Today
|
||||||
|
|
||||||
|
Every capability below is exercised by unit tests, an in-namespace end-to-end scan, and read-only audit passes.
|
||||||
|
|
||||||
|
**Scanning**
|
||||||
|
- Stateless TCP SYN scan over IPv4 and IPv6 via a raw `AF_PACKET` + `PACKET_TX_RING` + `PACKET_QDISC_BYPASS` datapath
|
||||||
|
- UDP scan with per-protocol payloads, ICMP type-3 code-3 classified as closed, silence reported honestly as `open|filtered`
|
||||||
|
- TCP connect() scan (`--connect`) for unprivileged or raw-blocked environments, IPv4 and IPv6
|
||||||
|
- Cloud and VM raw-send auto-detection (`--backend auto`): a two-socket self-probe detects hypervisors that silently drop raw sends and falls back to connect mode with a clear notice
|
||||||
|
|
||||||
|
**Statelessness and coverage**
|
||||||
|
- SipHash SYN-cookie identity in the TCP sequence number, validated by `ack == cookie +% 1`
|
||||||
|
- zmap-style multiplicative cyclic-group address permutation, seeded per scan from the OS CSPRNG, with the prime computed at runtime for the exact target space
|
||||||
|
- Token-bucket rate control, default 10,000 pps, responsible by default
|
||||||
|
- An RFC 6890 reserved-range exclusion floor that cannot be overridden
|
||||||
|
|
||||||
|
**Detection and evasion**
|
||||||
|
- Service and banner detection (`--banners`): a two-phase NULL-probe plus HTTP grab on open ports; TLS is detected, not decrypted; there is no JA4 (a dedicated Rust tool covers that)
|
||||||
|
- A stealth suite gated behind `--authorized-scan`: OS-realistic SYN templates, `fin|null|xmas|maimon|ack|window` scan types, Poisson jitter, source-port rotation, decoys, and scoped RST suppression
|
||||||
|
|
||||||
|
**Acceleration and output**
|
||||||
|
- An `AF_XDP` TX backend behind `-Dxdp` (pure-syscall UMEM and rings, with a zero-copy then `XDP_SKB` then `AF_PACKET` selection ladder)
|
||||||
|
- A truecolor live dashboard and a clean results table on stderr, with NDJSON on stdout (`--json`) so results stay greppable
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -fsSL https://angelamos.com/zingela/install.sh | bash
|
||||||
|
zingela scan --target 192.0.2.0/24 --ports 80,443 --rate 20000
|
||||||
|
```
|
||||||
|
|
||||||
|
One command takes a fresh Linux box to `zingela` on your PATH. The installer downloads a prebuilt static musl binary when a release is available and otherwise builds from source (fetching Zig 0.16 if it is missing), then grants `cap_net_raw,cap_net_admin` so raw scans run without sudo.
|
||||||
|
|
||||||
|
The target shown, `192.0.2.0/24`, is a reserved documentation range (RFC 5737) that zingela skips by design, so it reports zero targets. Substitute a range you own or are authorized to scan. For an unprivileged scan that needs no capabilities, add `--connect`.
|
||||||
|
|
||||||
|
> [!TIP]
|
||||||
|
> This project uses [`just`](https://github.com/casey/just) as a command runner. Type `just` to see every recipe grouped by area: `just safe` for a ReleaseSafe build, `just test-all` for the full matrix, `just bench` for the hot-path numbers, `just dist` for the musl release binaries, `just setcap` to grant capabilities.
|
||||||
|
>
|
||||||
|
> Install: `curl -sSf https://just.systems/install.sh | bash -s -- --to ~/.local/bin`
|
||||||
|
|
||||||
|
## Learn
|
||||||
|
|
||||||
|
This project ships a full teaching track. Read it in order, or jump to what you need.
|
||||||
|
|
||||||
|
| Doc | What it covers |
|
||||||
|
|-----|----------------|
|
||||||
|
| [`learn/00-OVERVIEW.md`](learn/00-OVERVIEW.md) | What a mass scanner is, why stateless scanning exists, and a quick tour |
|
||||||
|
| [`learn/01-CONCEPTS.md`](learn/01-CONCEPTS.md) | The handshake, SYN cookies, the permutation, responsible scanning, with real breaches |
|
||||||
|
| [`learn/02-ARCHITECTURE.md`](learn/02-ARCHITECTURE.md) | The two-engine model, the module map, and the backend ladder, with diagrams |
|
||||||
|
| [`learn/03-IMPLEMENTATION.md`](learn/03-IMPLEMENTATION.md) | A code walkthrough, module by module |
|
||||||
|
| [`learn/MECHANICS.md`](learn/MECHANICS.md) | The cookie, checksum, cyclic group, dedup, and token bucket, byte by byte, with the measured numbers |
|
||||||
|
| [`learn/04-CHALLENGES.md`](learn/04-CHALLENGES.md) | Extension ideas, from a real AF_XDP benchmark to new probe modules |
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
Two cooperating machines: a stateless line-rate transmit and receive core that owns the packet hot path, and a memory-safe control plane that parses intent, paces the sending, and presents results.
|
||||||
|
|
||||||
|
```
|
||||||
|
you --> cli / scancmd control plane: parse, validate, select backend
|
||||||
|
|
|
||||||
|
+---------+---------+
|
||||||
|
v v
|
||||||
|
tx (transmit) rx (receive) data plane: no heap alloc after startup
|
||||||
|
targets.next() read frame
|
||||||
|
cookie.seq classify open / closed / filtered
|
||||||
|
template.stamp cookie.validateSynAck (ack == cookie + 1 ?)
|
||||||
|
ratelimit gate |
|
||||||
|
packet_io v
|
||||||
|
| dedup.insert --> output (dashboard / table / NDJSON)
|
||||||
|
v
|
||||||
|
AF_XDP --> AF_PACKET TX_RING --> the wire --> AF_PACKET RX
|
||||||
|
```
|
||||||
|
|
||||||
|
Nothing links the transmit column to the receive column except arithmetic: the cookie written into the sequence number is the same cookie recomputed on receipt. A reply without a valid cookie is dropped before it can pollute the results.
|
||||||
|
|
||||||
|
**Design decisions:** the address permutation is a multiplicative cyclic group (zmap's approach, uniform coverage), not masscan's BlackRock Feistel cipher, which the 2024 "Ten Years of ZMap" retrospective found finds fewer hosts due to randomization bias. The prime is computed at runtime for the exact scan size rather than drawn from a fixed table. Raw packet I/O goes straight through `std.os.linux`, never `std.posix`, which dropped its socket wrappers. The `AF_XDP` path is asymmetric: it accelerates transmit and pairs with an `AF_PACKET` receive, because replies are sparse and this avoids receive-side steering.
|
||||||
|
|
||||||
|
## Build and Test
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig build # debug build at zig-out/bin/zingela
|
||||||
|
zig build -Doptimize=ReleaseSafe # the shipped artifact
|
||||||
|
zig build -Dxdp=true # include the AF_XDP TX backend
|
||||||
|
zig build test # unit tests (240)
|
||||||
|
zig build bench # hot-path microbenchmarks
|
||||||
|
zig build release # static musl binaries for x86_64 + aarch64
|
||||||
|
just ci # build + test
|
||||||
|
```
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> Plain `zig build` produces a **Debug** binary; the shipped artifact is `--release=safe` (ReleaseSafe), which keeps every undefined-behavior check live as a fail-closed trap rather than silent corruption. Sending raw packets needs `CAP_NET_RAW` and `CAP_NET_ADMIN`: run `just setcap` (or `sudo setcap cap_net_raw,cap_net_admin=eip ./zig-out/bin/zingela`) once, then run without sudo. The capability is cleared whenever the binary is rebuilt, so reapply it after every build.
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
The hot path is CPU-cheap by design, which is the whole basis of the honest positioning above: the bottleneck is the kernel transmit path, never packet construction. These are measured with `zig build bench` on an Intel Core i7-14700KF, single core, ReleaseFast, with inputs varied per iteration and results folded into a printed sink so nothing is optimized away. Reproduce them with `just bench`.
|
||||||
|
|
||||||
|
| Operation | ns/op | Throughput |
|
||||||
|
|---|---|---|
|
||||||
|
| RFC 1071 checksum, scalar, 40 B header | 6.0 | 6.7 GB/s |
|
||||||
|
| RFC 1071 checksum, SIMD, 1500 B frame | 30.4 | 49 GB/s |
|
||||||
|
| SipHash SYN-cookie generate | 14.0 | 71 M/s |
|
||||||
|
| SipHash SYN-cookie validate | 14.8 | 68 M/s |
|
||||||
|
| SYN frame stamp (full Ethernet+IP+TCP) | 22.9 | 44 M/s |
|
||||||
|
| Cyclic-group address permutation step | 4.6 | 216 M/s |
|
||||||
|
| RX dedup insert | 15.4 | 65 M/s |
|
||||||
|
|
||||||
|
A single core builds about 44 million complete SYN frames per second, roughly twenty times faster than the `AF_PACKET` kernel can drain them at 1.5 to 2.5 Mpps. The CPU is never the wall. That is exactly why the only real path to higher throughput is bypassing the kernel with `AF_XDP`, and why there is no honest raw-pps advantage to claim on stock hardware.
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
zig-stateless-scanner/
|
||||||
|
├── build.zig # module graph, `release` (musl x2) + `bench` steps, version
|
||||||
|
├── build.zig.zon # package manifest
|
||||||
|
├── install.sh # one-shot curl|bash: prebuilt-first, source fallback, setcap
|
||||||
|
├── src/
|
||||||
|
│ ├── main.zig # entry: parse args, dispatch smoke / tx / scan
|
||||||
|
│ ├── cli.zig # argument parsing, help, the violet banner, --version
|
||||||
|
│ ├── scancmd.zig # the scan command: validate, select backend, launch engines
|
||||||
|
│ ├── targets.zig # cyclic-group permutation, primitive-root finder, exclusion floor
|
||||||
|
│ ├── numtheory.zig # modexp, primality, primitive-root test for the permutation
|
||||||
|
│ ├── packet.zig # wire headers, RFC 1071 checksum (scalar + @Vector), SYN presets
|
||||||
|
│ ├── cookie.zig # SipHash SYN-cookie generate + validate, v4 and v6
|
||||||
|
│ ├── template.zig # the SYN frame template stamped per target
|
||||||
|
│ ├── ratelimit.zig # the token-bucket pacer
|
||||||
|
│ ├── tx.zig rx.zig # the transmit and receive engines
|
||||||
|
│ ├── classify.zig # reply classification open / closed / filtered
|
||||||
|
│ ├── dedup.zig # the lockless open-addressed dedup table
|
||||||
|
│ ├── packet_io.zig # backend interface: afpacket.zig, afxdp.zig, connect.zig
|
||||||
|
│ ├── udp.zig # UDP payloads and UDP classification
|
||||||
|
│ ├── service.zig # opt-in banner and service detection (no JA4)
|
||||||
|
│ ├── stealth.zig # the --authorized-scan gated evasion features
|
||||||
|
│ ├── output.zig # live dashboard, results table, NDJSON
|
||||||
|
│ ├── netutil.zig ndp.zig rawprobe.zig # iface + gateway resolution, NDP, cloud probe
|
||||||
|
│ └── bench.zig # the hot-path microbenchmark harness
|
||||||
|
└── learn/ # the teaching track (this is public)
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[AGPL 3.0](LICENSE).
|
||||||
|
|
@ -0,0 +1,376 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// build.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const zingela_version = "0.0.0-m11";
|
||||||
|
|
||||||
|
const ReleaseTarget = struct {
|
||||||
|
query: std.Target.Query,
|
||||||
|
asset: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const release_targets = [_]ReleaseTarget{
|
||||||
|
.{ .query = .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }, .asset = "zingela-x86_64-linux-musl" },
|
||||||
|
.{ .query = .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl }, .asset = "zingela-aarch64-linux-musl" },
|
||||||
|
};
|
||||||
|
|
||||||
|
const Built = struct {
|
||||||
|
exe: *std.Build.Step.Compile,
|
||||||
|
test_mods: []const *std.Build.Module,
|
||||||
|
packet: *std.Build.Module,
|
||||||
|
cookie: *std.Build.Module,
|
||||||
|
targets: *std.Build.Module,
|
||||||
|
template: *std.Build.Module,
|
||||||
|
dedup: *std.Build.Module,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn build(b: *std.Build) void {
|
||||||
|
const target = b.standardTargetOptions(.{});
|
||||||
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
|
const xdp_enabled = b.option(bool, "xdp", "Enable the AF_XDP TX backend (pure-syscall, no libxdp; needs CAP_NET_ADMIN at runtime)") orelse false;
|
||||||
|
|
||||||
|
const host = buildZingela(b, target, optimize, xdp_enabled);
|
||||||
|
b.installArtifact(host.exe);
|
||||||
|
|
||||||
|
const run_cmd = b.addRunArtifact(host.exe);
|
||||||
|
run_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
if (b.args) |args| run_cmd.addArgs(args);
|
||||||
|
const run_step = b.step("run", "Run zingela");
|
||||||
|
run_step.dependOn(&run_cmd.step);
|
||||||
|
|
||||||
|
const smoke_cmd = b.addSystemCommand(&.{b.getInstallPath(.bin, "zingela")});
|
||||||
|
smoke_cmd.addArg("smoke");
|
||||||
|
if (b.args) |args| smoke_cmd.addArgs(args);
|
||||||
|
smoke_cmd.step.dependOn(b.getInstallStep());
|
||||||
|
const smoke_step = b.step("smoke", "AF_PACKET ground-truth smoke on the installed binary (setcap it first)");
|
||||||
|
smoke_step.dependOn(&smoke_cmd.step);
|
||||||
|
|
||||||
|
const test_step = b.step("test", "Run unit tests");
|
||||||
|
for (host.test_mods) |mod| {
|
||||||
|
const t = b.addTest(.{ .root_module = mod });
|
||||||
|
const rt = b.addRunArtifact(t);
|
||||||
|
test_step.dependOn(&rt.step);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bench_src = buildZingela(b, target, .ReleaseFast, xdp_enabled);
|
||||||
|
const bench_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/bench.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = .ReleaseFast,
|
||||||
|
});
|
||||||
|
bench_mod.addImport("packet", bench_src.packet);
|
||||||
|
bench_mod.addImport("cookie", bench_src.cookie);
|
||||||
|
bench_mod.addImport("targets", bench_src.targets);
|
||||||
|
bench_mod.addImport("template", bench_src.template);
|
||||||
|
bench_mod.addImport("dedup", bench_src.dedup);
|
||||||
|
const bench_exe = b.addExecutable(.{ .name = "zingela-bench", .root_module = bench_mod });
|
||||||
|
const bench_run = b.addRunArtifact(bench_exe);
|
||||||
|
if (b.args) |args| bench_run.addArgs(args);
|
||||||
|
const bench_step = b.step("bench", "Run the hot-path microbenchmarks (ReleaseFast, measured on this host)");
|
||||||
|
bench_step.dependOn(&bench_run.step);
|
||||||
|
|
||||||
|
const release_step = b.step("release", "Build static musl release binaries for every distribution target");
|
||||||
|
for (release_targets) |rt| {
|
||||||
|
const resolved = b.resolveTargetQuery(rt.query);
|
||||||
|
const built = buildZingela(b, resolved, .ReleaseSafe, xdp_enabled);
|
||||||
|
const inst = b.addInstallArtifact(built.exe, .{
|
||||||
|
.dest_dir = .{ .override = .{ .custom = "release" } },
|
||||||
|
.dest_sub_path = rt.asset,
|
||||||
|
});
|
||||||
|
release_step.dependOn(&inst.step);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buildZingela(
|
||||||
|
b: *std.Build,
|
||||||
|
target: std.Build.ResolvedTarget,
|
||||||
|
optimize: std.builtin.OptimizeMode,
|
||||||
|
xdp_enabled: bool,
|
||||||
|
) Built {
|
||||||
|
const opts = b.addOptions();
|
||||||
|
opts.addOption([]const u8, "version", zingela_version);
|
||||||
|
opts.addOption(bool, "xdp", xdp_enabled);
|
||||||
|
const build_config_mod = opts.createModule();
|
||||||
|
|
||||||
|
const packet_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/packet.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const cli_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/cli.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
cli_mod.addImport("build_config", build_config_mod);
|
||||||
|
|
||||||
|
const smoke_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/smoke.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
smoke_mod.addImport("packet", packet_mod);
|
||||||
|
|
||||||
|
const cookie_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/cookie.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const numtheory_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/numtheory.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const targets_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/targets.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
targets_mod.addImport("numtheory", numtheory_mod);
|
||||||
|
|
||||||
|
const ratelimit_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/ratelimit.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const template_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/template.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
template_mod.addImport("packet", packet_mod);
|
||||||
|
template_mod.addImport("cookie", cookie_mod);
|
||||||
|
|
||||||
|
const segment_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/segment.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
segment_mod.addImport("packet", packet_mod);
|
||||||
|
|
||||||
|
const regex_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/regex.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const probe_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/probe.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
probe_mod.addImport("regex", regex_mod);
|
||||||
|
|
||||||
|
const service_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/service.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
service_mod.addImport("packet", packet_mod);
|
||||||
|
service_mod.addImport("cookie", cookie_mod);
|
||||||
|
service_mod.addImport("segment", segment_mod);
|
||||||
|
service_mod.addImport("probe", probe_mod);
|
||||||
|
|
||||||
|
const payloads_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/payloads.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const udp_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/udp.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
udp_mod.addImport("packet", packet_mod);
|
||||||
|
udp_mod.addImport("cookie", cookie_mod);
|
||||||
|
udp_mod.addImport("payloads", payloads_mod);
|
||||||
|
|
||||||
|
const afpacket_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/afpacket.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
afpacket_mod.addImport("packet", packet_mod);
|
||||||
|
|
||||||
|
const xdp_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/xdp.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const afxdp_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/afxdp.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
afxdp_mod.addImport("xdp", xdp_mod);
|
||||||
|
|
||||||
|
const packet_io_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/packet_io.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
packet_io_mod.addImport("afpacket", afpacket_mod);
|
||||||
|
packet_io_mod.addImport("afxdp", afxdp_mod);
|
||||||
|
packet_io_mod.addImport("build_config", build_config_mod);
|
||||||
|
|
||||||
|
const tx_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/tx.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
tx_mod.addImport("targets", targets_mod);
|
||||||
|
tx_mod.addImport("template", template_mod);
|
||||||
|
tx_mod.addImport("ratelimit", ratelimit_mod);
|
||||||
|
tx_mod.addImport("cookie", cookie_mod);
|
||||||
|
tx_mod.addImport("packet", packet_mod);
|
||||||
|
|
||||||
|
const classify_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/classify.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
classify_mod.addImport("packet", packet_mod);
|
||||||
|
classify_mod.addImport("cookie", cookie_mod);
|
||||||
|
|
||||||
|
const output_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/output.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
output_mod.addImport("classify", classify_mod);
|
||||||
|
output_mod.addImport("packet", packet_mod);
|
||||||
|
cli_mod.addImport("output", output_mod);
|
||||||
|
|
||||||
|
const connect_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/connect.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
connect_mod.addImport("classify", classify_mod);
|
||||||
|
connect_mod.addImport("packet", packet_mod);
|
||||||
|
connect_mod.addImport("targets", targets_mod);
|
||||||
|
connect_mod.addImport("ratelimit", ratelimit_mod);
|
||||||
|
connect_mod.addImport("output", output_mod);
|
||||||
|
|
||||||
|
const dedup_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/dedup.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const rx_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/rx.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
rx_mod.addImport("classify", classify_mod);
|
||||||
|
rx_mod.addImport("dedup", dedup_mod);
|
||||||
|
rx_mod.addImport("cookie", cookie_mod);
|
||||||
|
|
||||||
|
const netutil_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/netutil.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
const rawprobe_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/rawprobe.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
|
targets_mod.addImport("netutil", netutil_mod);
|
||||||
|
tx_mod.addImport("netutil", netutil_mod);
|
||||||
|
|
||||||
|
const ndp_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/ndp.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
ndp_mod.addImport("packet", packet_mod);
|
||||||
|
|
||||||
|
const stealth_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/stealth.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
stealth_mod.addImport("packet", packet_mod);
|
||||||
|
stealth_mod.addImport("netutil", netutil_mod);
|
||||||
|
|
||||||
|
const txcmd_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/txcmd.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
txcmd_mod.addImport("targets", targets_mod);
|
||||||
|
txcmd_mod.addImport("template", template_mod);
|
||||||
|
txcmd_mod.addImport("ratelimit", ratelimit_mod);
|
||||||
|
txcmd_mod.addImport("packet_io", packet_io_mod);
|
||||||
|
txcmd_mod.addImport("cookie", cookie_mod);
|
||||||
|
txcmd_mod.addImport("tx", tx_mod);
|
||||||
|
txcmd_mod.addImport("netutil", netutil_mod);
|
||||||
|
txcmd_mod.addImport("stealth", stealth_mod);
|
||||||
|
|
||||||
|
const scancmd_mod = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/scancmd.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
scancmd_mod.addImport("targets", targets_mod);
|
||||||
|
scancmd_mod.addImport("template", template_mod);
|
||||||
|
scancmd_mod.addImport("udp", udp_mod);
|
||||||
|
scancmd_mod.addImport("ratelimit", ratelimit_mod);
|
||||||
|
scancmd_mod.addImport("packet_io", packet_io_mod);
|
||||||
|
scancmd_mod.addImport("cookie", cookie_mod);
|
||||||
|
scancmd_mod.addImport("tx", tx_mod);
|
||||||
|
scancmd_mod.addImport("rx", rx_mod);
|
||||||
|
scancmd_mod.addImport("dedup", dedup_mod);
|
||||||
|
scancmd_mod.addImport("netutil", netutil_mod);
|
||||||
|
scancmd_mod.addImport("output", output_mod);
|
||||||
|
scancmd_mod.addImport("stealth", stealth_mod);
|
||||||
|
scancmd_mod.addImport("service", service_mod);
|
||||||
|
scancmd_mod.addImport("connect", connect_mod);
|
||||||
|
scancmd_mod.addImport("rawprobe", rawprobe_mod);
|
||||||
|
scancmd_mod.addImport("ndp", ndp_mod);
|
||||||
|
|
||||||
|
const exe = b.addExecutable(.{
|
||||||
|
.name = "zingela",
|
||||||
|
.root_module = b.createModule(.{
|
||||||
|
.root_source_file = b.path("src/main.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
.strip = optimize != .Debug,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
exe.root_module.addImport("cli", cli_mod);
|
||||||
|
exe.root_module.addImport("smoke", smoke_mod);
|
||||||
|
exe.root_module.addImport("txcmd", txcmd_mod);
|
||||||
|
exe.root_module.addImport("scancmd", scancmd_mod);
|
||||||
|
|
||||||
|
const test_mods = b.allocator.dupe(*std.Build.Module, &.{
|
||||||
|
packet_mod, cli_mod, smoke_mod, cookie_mod, numtheory_mod,
|
||||||
|
targets_mod, ratelimit_mod, template_mod, segment_mod, regex_mod,
|
||||||
|
probe_mod, service_mod, payloads_mod, udp_mod, afpacket_mod,
|
||||||
|
xdp_mod, afxdp_mod, packet_io_mod, tx_mod, txcmd_mod,
|
||||||
|
classify_mod, dedup_mod, rx_mod, netutil_mod, rawprobe_mod,
|
||||||
|
ndp_mod, stealth_mod, output_mod, connect_mod, scancmd_mod,
|
||||||
|
}) catch @panic("OOM");
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.exe = exe,
|
||||||
|
.test_mods = test_mods,
|
||||||
|
.packet = packet_mod,
|
||||||
|
.cookie = cookie_mod,
|
||||||
|
.targets = targets_mod,
|
||||||
|
.template = template_mod,
|
||||||
|
.dedup = dedup_mod,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// build.zig.zon
|
||||||
|
.{
|
||||||
|
.name = .zingela,
|
||||||
|
.version = "0.0.0",
|
||||||
|
.minimum_zig_version = "0.16.0",
|
||||||
|
.fingerprint = 0xc79d74b14130bdf4,
|
||||||
|
.dependencies = .{},
|
||||||
|
.paths = .{
|
||||||
|
"build.zig",
|
||||||
|
"build.zig.zon",
|
||||||
|
"src",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,282 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# ©AngelaMos | 2026
|
||||||
|
# install.sh
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# --- config ---------------------------------------------------------------
|
||||||
|
REPO_OWNER="CarterPerez-dev"
|
||||||
|
REPO_NAME="Cybersecurity-Projects"
|
||||||
|
PROJECT_SUBDIR="PROJECTS/advanced/zig-stateless-scanner"
|
||||||
|
BINARY="zingela"
|
||||||
|
TAGLINE="stateless mass TCP/UDP scanner - single static Zig binary"
|
||||||
|
REPO_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}.git"
|
||||||
|
INSTALL_DIR="${ZINGELA_INSTALL_DIR:-$HOME/.local/bin}"
|
||||||
|
DEFAULT_BRANCH="main"
|
||||||
|
ZIG_VER="0.16.0"
|
||||||
|
ZIG_MIN="0.16.0"
|
||||||
|
DO_SETCAP=1
|
||||||
|
|
||||||
|
# --- colors (gated so `| bash`, logs and CI stay clean) -------------------
|
||||||
|
if [ -t 2 ] && [ -z "${NO_COLOR:-}" ]; then
|
||||||
|
BOLD=$'\033[1m'; DIM=$'\033[2m'; RED=$'\033[31m'; GREEN=$'\033[32m'
|
||||||
|
YELLOW=$'\033[33m'; VIOLET=$'\033[38;2;139;92;246m'; RESET=$'\033[0m'
|
||||||
|
else
|
||||||
|
BOLD=""; DIM=""; RED=""; GREEN=""; YELLOW=""; VIOLET=""; RESET=""
|
||||||
|
fi
|
||||||
|
|
||||||
|
info() { printf '%s\n' " ${VIOLET}+${RESET} $*" >&2; }
|
||||||
|
ok() { printf '%s\n' " ${GREEN}+${RESET} $*" >&2; }
|
||||||
|
warn() { printf '%s\n' " ${YELLOW}!${RESET} $*" >&2; }
|
||||||
|
die() { printf '%s\n' " ${RED}x $*${RESET}" >&2; exit 1; }
|
||||||
|
header(){ printf '\n%s\n\n' "${BOLD}${VIOLET}--- $* ---${RESET}" >&2; }
|
||||||
|
have() { command -v "$1" >/dev/null 2>&1; }
|
||||||
|
|
||||||
|
trap 'printf "%s\n" "${RED}x install failed${RESET}" >&2' ERR
|
||||||
|
TMP_DIR=""
|
||||||
|
cleanup() { [ -n "$TMP_DIR" ] && rm -rf "$TMP_DIR"; return 0; }
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
banner() {
|
||||||
|
printf '%s' "${VIOLET}${BOLD}" >&2
|
||||||
|
cat >&2 <<'ART'
|
||||||
|
____ _ _
|
||||||
|
|_ /(_) _ _ __ _ ___| | __ _
|
||||||
|
/ / | || ' \ / _` |/ -_) |/ _` |
|
||||||
|
/___||_||_||_|\__, |\___|_|\__,_|
|
||||||
|
|___/
|
||||||
|
ART
|
||||||
|
printf '%s\n' "${RESET}" >&2
|
||||||
|
printf '%s\n' " ${DIM}${TAGLINE}${RESET}" >&2
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- privilege + package manager fan --------------------------------------
|
||||||
|
SUDO=""
|
||||||
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
|
if have sudo; then SUDO="sudo"; fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
pkg_install() {
|
||||||
|
if have apt-get; then $SUDO apt-get update -y || warn "apt update had errors; continuing"
|
||||||
|
$SUDO apt-get install -y --no-install-recommends "$@"
|
||||||
|
elif have dnf; then $SUDO dnf install -y "$@"
|
||||||
|
elif have pacman; then $SUDO pacman -S --needed --noconfirm "$@"
|
||||||
|
elif have zypper; then $SUDO zypper install -y "$@"
|
||||||
|
elif have apk; then $SUDO apk add "$@"
|
||||||
|
else die "no known package manager. Install manually: $*"; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
download() {
|
||||||
|
if have curl; then curl -fsSL "$1" -o "$2" || return 1
|
||||||
|
elif have wget; then wget -qO "$2" "$1" || return 1
|
||||||
|
else die "need curl or wget"; fi
|
||||||
|
}
|
||||||
|
|
||||||
|
version_ge() { [ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | head -1)" = "$2" ]; }
|
||||||
|
|
||||||
|
# --- args -----------------------------------------------------------------
|
||||||
|
usage() {
|
||||||
|
cat >&2 <<USAGE
|
||||||
|
install.sh - install ${BINARY}
|
||||||
|
|
||||||
|
./install.sh [options]
|
||||||
|
curl -fsSL https://angelamos.com/${BINARY}/install.sh | bash
|
||||||
|
|
||||||
|
options:
|
||||||
|
--prefix DIR install dir (default: ${INSTALL_DIR})
|
||||||
|
--no-setcap skip granting raw-socket capabilities (use sudo or --connect instead)
|
||||||
|
-h, --help this help
|
||||||
|
USAGE
|
||||||
|
}
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
--prefix) INSTALL_DIR="$2"; shift 2 ;;
|
||||||
|
--prefix=*) INSTALL_DIR="${1#*=}"; shift ;;
|
||||||
|
--no-setcap) DO_SETCAP=0; shift ;;
|
||||||
|
-h|--help) usage; exit 0 ;;
|
||||||
|
*) die "unknown option: $1 (try --help)" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# --- OS / arch (Linux only: raw AF_PACKET + std.os.linux) -----------------
|
||||||
|
OS="$(uname -s)"; ARCH="$(uname -m)"
|
||||||
|
case "$OS" in
|
||||||
|
Linux) ;;
|
||||||
|
Darwin) die "${BINARY} is Linux-only (raw AF_PACKET / XDP). Run it in a Linux VM or WSL2." ;;
|
||||||
|
MINGW*|MSYS*|CYGWIN*) die "${BINARY} is Linux-only. Use WSL2." ;;
|
||||||
|
*) die "unsupported OS: $OS (${BINARY} is Linux-only)" ;;
|
||||||
|
esac
|
||||||
|
case "$ARCH" in
|
||||||
|
x86_64|amd64) MUSL_ARCH="x86_64" ;;
|
||||||
|
aarch64|arm64) MUSL_ARCH="aarch64" ;;
|
||||||
|
*) die "unsupported arch: $ARCH (${BINARY} builds for x86_64 and aarch64)" ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# --- bootstrap: works in-clone OR piped from a domain ---------------------
|
||||||
|
clone_repo() {
|
||||||
|
local cache="$1"
|
||||||
|
if git clone --depth 1 --filter=blob:none --sparse --branch "$DEFAULT_BRANCH" --quiet "$REPO_URL" "$cache" 2>/dev/null \
|
||||||
|
&& git -C "$cache" sparse-checkout set "$PROJECT_SUBDIR" 2>/dev/null; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
rm -rf "$cache"
|
||||||
|
git clone --depth 1 --branch "$DEFAULT_BRANCH" --quiet "$REPO_URL" "$cache"
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve_project_dir() {
|
||||||
|
if [ -f "./build.zig" ] && [ -d "./src" ]; then pwd; return; fi
|
||||||
|
if [ -f "./${PROJECT_SUBDIR}/build.zig" ]; then printf '%s\n' "$(pwd)/${PROJECT_SUBDIR}"; return; fi
|
||||||
|
local self="${BASH_SOURCE[0]:-}"
|
||||||
|
if [ -n "$self" ] && [ -f "$(dirname "$self")/build.zig" ]; then (cd "$(dirname "$self")" && pwd); return; fi
|
||||||
|
if ! have git; then warn "git missing - installing it"; pkg_install git; fi
|
||||||
|
have git || die "could not install git; install it then re-run"
|
||||||
|
local cache="${XDG_CACHE_HOME:-$HOME/.cache}/${BINARY}-src"
|
||||||
|
if [ -d "$cache/.git" ]; then
|
||||||
|
info "updating cached clone at $cache"
|
||||||
|
git -C "$cache" pull --ff-only --quiet 2>/dev/null || warn "pull failed; using existing clone"
|
||||||
|
else
|
||||||
|
info "cloning ${REPO_URL} (sparse: ${PROJECT_SUBDIR})"
|
||||||
|
clone_repo "$cache"
|
||||||
|
fi
|
||||||
|
printf '%s\n' "$cache/${PROJECT_SUBDIR}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- toolchain: Zig 0.16, auto-fetched if missing/too old -----------------
|
||||||
|
need_toolchain() {
|
||||||
|
if have zig; then
|
||||||
|
local v; v="$(zig version 2>/dev/null | head -1)"
|
||||||
|
if version_ge "$v" "$ZIG_MIN"; then ok "zig $v"; return; fi
|
||||||
|
warn "zig $v is older than ${ZIG_MIN}; fetching a private ${ZIG_VER}"
|
||||||
|
else
|
||||||
|
info "zig not found; fetching ${ZIG_VER}"
|
||||||
|
fi
|
||||||
|
local zroot="zig-${MUSL_ARCH}-linux-${ZIG_VER}"
|
||||||
|
local zdir="${XDG_CACHE_HOME:-$HOME/.cache}/${BINARY}-zig"
|
||||||
|
if [ ! -x "$zdir/$zroot/zig" ]; then
|
||||||
|
mkdir -p "$zdir"
|
||||||
|
TMP_DIR="${TMP_DIR:-$(mktemp -d)}"
|
||||||
|
info "downloading https://ziglang.org/download/${ZIG_VER}/${zroot}.tar.xz"
|
||||||
|
download "https://ziglang.org/download/${ZIG_VER}/${zroot}.tar.xz" "$TMP_DIR/zig.tar.xz" \
|
||||||
|
|| die "could not download Zig ${ZIG_VER}"
|
||||||
|
if ! tar -xf "$TMP_DIR/zig.tar.xz" -C "$zdir" 2>/dev/null; then
|
||||||
|
pkg_install xz-utils 2>/dev/null || pkg_install xz 2>/dev/null || true
|
||||||
|
tar -xf "$TMP_DIR/zig.tar.xz" -C "$zdir" || die "could not extract Zig (need tar + xz)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
export PATH="$zdir/$zroot:$PATH"
|
||||||
|
have zig || die "zig still not on PATH after fetch"
|
||||||
|
ok "zig $(zig version)"
|
||||||
|
}
|
||||||
|
|
||||||
|
build_from_source() {
|
||||||
|
info "zig build --release=safe (compiling ${BINARY}; this can take a minute)"
|
||||||
|
zig build --release=safe
|
||||||
|
[ -x "zig-out/bin/${BINARY}" ] || die "build did not produce zig-out/bin/${BINARY}"
|
||||||
|
mkdir -p "$INSTALL_DIR"
|
||||||
|
install -m 0755 "zig-out/bin/${BINARY}" "$INSTALL_DIR/$BINARY"
|
||||||
|
ok "built + installed ${INSTALL_DIR}/${BINARY}"
|
||||||
|
}
|
||||||
|
|
||||||
|
try_prebuilt() {
|
||||||
|
local tag asset url
|
||||||
|
tag="$(download "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases" /dev/stdout 2>/dev/null \
|
||||||
|
| grep '"tag_name":' | grep -o '"zingela-[^"]*"' | head -1 | tr -d '"')" || true
|
||||||
|
[ -n "$tag" ] || { info "no published ${BINARY} release yet - building from source"; return 1; }
|
||||||
|
asset="zingela-${MUSL_ARCH}-linux-musl"
|
||||||
|
url="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${tag}/${asset}"
|
||||||
|
TMP_DIR="${TMP_DIR:-$(mktemp -d)}"
|
||||||
|
info "downloading prebuilt ${tag} (${asset})"
|
||||||
|
download "$url" "$TMP_DIR/$BINARY" || { warn "no prebuilt ${asset} in ${tag}; building from source"; return 1; }
|
||||||
|
mkdir -p "$INSTALL_DIR"
|
||||||
|
install -m 0755 "$TMP_DIR/$BINARY" "$INSTALL_DIR/$BINARY"
|
||||||
|
ok "installed prebuilt ${tag} -> ${INSTALL_DIR}/${BINARY}"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- PATH wiring ----------------------------------------------------------
|
||||||
|
wire_path() {
|
||||||
|
case ":$PATH:" in *":$INSTALL_DIR:"*) ok "$INSTALL_DIR already on PATH"; return ;; esac
|
||||||
|
local shell rc=""
|
||||||
|
shell="$(basename "${SHELL:-bash}")"
|
||||||
|
case "$shell" in
|
||||||
|
zsh) rc="$HOME/.zshrc" ;;
|
||||||
|
fish) mkdir -p "$HOME/.config/fish/conf.d"
|
||||||
|
echo "fish_add_path $INSTALL_DIR" > "$HOME/.config/fish/conf.d/${BINARY}.fish"
|
||||||
|
ok "added to fish conf.d" ;;
|
||||||
|
bash) rc="$HOME/.bashrc"; [ -f "$rc" ] || rc="$HOME/.bash_profile" ;;
|
||||||
|
*) rc="$HOME/.profile" ;;
|
||||||
|
esac
|
||||||
|
if [ -n "$rc" ] && ! grep -q "$INSTALL_DIR" "$rc" 2>/dev/null; then
|
||||||
|
printf '\nexport PATH="%s:$PATH"\n' "$INSTALL_DIR" >> "$rc"
|
||||||
|
ok "added $INSTALL_DIR to PATH in $rc"
|
||||||
|
fi
|
||||||
|
export PATH="$INSTALL_DIR:$PATH"
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- raw-socket capabilities (so `zingela scan` needs no sudo) ------------
|
||||||
|
grant_caps() {
|
||||||
|
local dest="$INSTALL_DIR/$BINARY"
|
||||||
|
if [ "$DO_SETCAP" -ne 1 ]; then
|
||||||
|
warn "skipping setcap (--no-setcap). Raw scans need: ${SUDO:+sudo }setcap cap_net_raw,cap_net_admin=eip \"$dest\""
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if ! have setcap; then
|
||||||
|
warn "setcap not found. For raw scans, install libcap then grant caps:"
|
||||||
|
warn " ${SUDO:+sudo }apt-get install -y libcap2-bin (or dnf/pacman equivalent)"
|
||||||
|
warn " ${SUDO:+sudo }setcap cap_net_raw,cap_net_admin=eip \"$dest\""
|
||||||
|
warn "or scan unprivileged right now: ${BINARY} scan --connect --target <cidr> --ports <list>"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if $SUDO setcap cap_net_raw,cap_net_admin=eip "$dest"; then
|
||||||
|
ok "granted CAP_NET_RAW + CAP_NET_ADMIN - raw scans run WITHOUT sudo"
|
||||||
|
else
|
||||||
|
warn "could not setcap (needs root). Enable raw scans without sudo via:"
|
||||||
|
warn " ${SUDO:+sudo }setcap cap_net_raw,cap_net_admin=eip \"$dest\""
|
||||||
|
warn "until then: run under sudo, or use ${BINARY} scan --connect (no caps needed)"
|
||||||
|
fi
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# --- main -----------------------------------------------------------------
|
||||||
|
# main() runs only after bash has read the whole file, and `</dev/null`
|
||||||
|
# denies children the pipe, so `curl ... | bash` never stops early.
|
||||||
|
main() {
|
||||||
|
banner
|
||||||
|
if have "$BINARY"; then info "existing install at $(command -v "$BINARY") - updating"; fi
|
||||||
|
|
||||||
|
if ! try_prebuilt; then
|
||||||
|
header "Build from source"
|
||||||
|
PROJECT_DIR="$(resolve_project_dir)"
|
||||||
|
cd "$PROJECT_DIR"
|
||||||
|
need_toolchain
|
||||||
|
build_from_source
|
||||||
|
fi
|
||||||
|
|
||||||
|
wire_path
|
||||||
|
grant_caps
|
||||||
|
|
||||||
|
header "Verify"
|
||||||
|
if have "$BINARY"; then
|
||||||
|
ok "$BINARY -> $(command -v "$BINARY")"
|
||||||
|
"$BINARY" --version 2>/dev/null || true
|
||||||
|
else
|
||||||
|
warn "installed to $INSTALL_DIR but not on PATH yet - open a new shell"
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf '\n%s\n\n' " ${GREEN}${BOLD}${BINARY} is ready.${RESET}" >&2
|
||||||
|
cat >&2 <<FOOTER
|
||||||
|
${DIM}quick start:${RESET}
|
||||||
|
${VIOLET}${BINARY} --help${RESET}
|
||||||
|
${VIOLET}${BINARY} scan --target 192.0.2.0/24 --ports 80,443 --rate 20000${RESET}
|
||||||
|
${VIOLET}${BINARY} scan --connect --target 192.0.2.0/24 --ports 22${RESET} ${DIM}(no caps needed)${RESET}
|
||||||
|
|
||||||
|
${DIM}the example target 192.0.2.0/24 is a reserved documentation range that ${BINARY}
|
||||||
|
skips by design; replace it with a range you are authorized to scan.${RESET}
|
||||||
|
${DIM}note: capabilities are cleared whenever the binary is replaced, so re-run this
|
||||||
|
installer (or the setcap line) after every upgrade.${RESET}
|
||||||
|
${DIM}docs: https://github.com/${REPO_OWNER}/${REPO_NAME}/tree/main/${PROJECT_SUBDIR}${RESET}
|
||||||
|
FOOTER
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@" </dev/null
|
||||||
|
|
@ -0,0 +1,147 @@
|
||||||
|
# =============================================================================
|
||||||
|
# ©AngelaMos | 2026
|
||||||
|
# justfile
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
set shell := ["bash", "-uc"]
|
||||||
|
|
||||||
|
project := file_name(justfile_directory())
|
||||||
|
version := `git describe --tags --always 2>/dev/null || echo "dev"`
|
||||||
|
bin := justfile_directory() / "zig-out/bin/zingela"
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Default
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
default:
|
||||||
|
@just --list --unsorted
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Build (plain zig build is Debug; the shipped artifact is ReleaseSafe)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
[group('build')]
|
||||||
|
build:
|
||||||
|
zig build
|
||||||
|
|
||||||
|
[group('build')]
|
||||||
|
safe:
|
||||||
|
zig build -Doptimize=ReleaseSafe
|
||||||
|
|
||||||
|
[group('build')]
|
||||||
|
fast:
|
||||||
|
zig build -Doptimize=ReleaseFast
|
||||||
|
|
||||||
|
# ReleaseSafe with the AF_XDP TX backend compiled in
|
||||||
|
[group('build')]
|
||||||
|
xdp:
|
||||||
|
zig build -Doptimize=ReleaseSafe -Dxdp=true
|
||||||
|
|
||||||
|
# static musl binaries for every distribution target -> zig-out/release/
|
||||||
|
[group('build')]
|
||||||
|
dist:
|
||||||
|
zig build release
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Test
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
[group('test')]
|
||||||
|
test:
|
||||||
|
zig build test
|
||||||
|
|
||||||
|
[group('test')]
|
||||||
|
test-verbose:
|
||||||
|
zig build test --summary all
|
||||||
|
|
||||||
|
# the full matrix: Debug + ReleaseSafe, each with and without -Dxdp
|
||||||
|
[group('test')]
|
||||||
|
test-all:
|
||||||
|
zig build test --summary all
|
||||||
|
zig build test -Doptimize=ReleaseSafe --summary all
|
||||||
|
zig build test -Dxdp=true --summary all
|
||||||
|
zig build test -Doptimize=ReleaseSafe -Dxdp=true --summary all
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Bench
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# hot-path microbenchmarks, ReleaseFast, measured on this host
|
||||||
|
[group('bench')]
|
||||||
|
bench:
|
||||||
|
zig build bench
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Run
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# run the built binary with any args, e.g. `just run --help`
|
||||||
|
[group('run')]
|
||||||
|
run *ARGS: build
|
||||||
|
{{bin}} {{ARGS}}
|
||||||
|
|
||||||
|
# SYN/UDP scan; pass flags, e.g. `just scan --target 192.0.2.0/24 --ports 80`
|
||||||
|
[group('run')]
|
||||||
|
scan *ARGS: build
|
||||||
|
{{bin}} scan {{ARGS}}
|
||||||
|
|
||||||
|
# grant raw-socket caps so scans run without sudo (reapply after every rebuild)
|
||||||
|
[group('run')]
|
||||||
|
setcap: build
|
||||||
|
sudo setcap cap_net_raw,cap_net_admin=eip {{bin}}
|
||||||
|
|
||||||
|
# AF_PACKET ground-truth smoke on the installed binary (needs caps: run `just setcap`)
|
||||||
|
[group('run')]
|
||||||
|
smoke:
|
||||||
|
zig build smoke
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Lint and Format
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
[group('lint')]
|
||||||
|
fmt:
|
||||||
|
zig fmt build.zig build.zig.zon src
|
||||||
|
|
||||||
|
[group('lint')]
|
||||||
|
fmt-check:
|
||||||
|
zig fmt --check build.zig build.zig.zon src
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Install
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
# one-shot install to PATH + setcap (prebuilt-first, source fallback)
|
||||||
|
[group('install')]
|
||||||
|
install:
|
||||||
|
./install.sh
|
||||||
|
|
||||||
|
[group('install')]
|
||||||
|
uninstall:
|
||||||
|
./uninstall.sh
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# CI / Quality (the always-green gate; fmt-check and smoke are separate:
|
||||||
|
# smoke needs raw-socket caps, and fmt has pre-existing debt to clear)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
[group('ci')]
|
||||||
|
ci: build test
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Utilities
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
[group('util')]
|
||||||
|
info:
|
||||||
|
@echo "Project: {{project}}"
|
||||||
|
@echo "Version: {{version}}"
|
||||||
|
@echo "Binary: {{bin}}"
|
||||||
|
@echo "OS: {{os()}} ({{arch()}})"
|
||||||
|
@zig version | xargs -I{} echo "Zig: {}"
|
||||||
|
|
||||||
|
[group('util')]
|
||||||
|
clean:
|
||||||
|
-rm -rf zig-out
|
||||||
|
-rm -rf .zig-cache
|
||||||
|
@echo "Build artifacts cleaned"
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- 00-OVERVIEW.md -->
|
||||||
|
|
||||||
|
# zingela: Overview
|
||||||
|
|
||||||
|
zingela is a stateless, line-rate mass port scanner. You give it a range of addresses and ports, it sends one crafted probe to each, and it classifies every reply as open, closed, or filtered without ever holding a connection table. It is written in Zig 0.16, ships as a single static binary with no libpcap and no libgmp, and it is the direct descendant of two tools: masscan (Robert Graham, 2013) and zmap (Durumeric, Wustrow, and Halderman, USENIX Security 2013).
|
||||||
|
|
||||||
|
This folder teaches how it works, from the security theory down to the bytes on the wire.
|
||||||
|
|
||||||
|
## Why stateless scanning exists
|
||||||
|
|
||||||
|
A normal TCP client tracks every connection: a socket, a state machine, a retransmit timer, a receive buffer. That is fine for a few thousand connections. It falls apart at internet scale. To probe all 3.7 billion routable IPv4 addresses on one port, a stateful scanner would need billions of live socket structures and the kernel bookkeeping to match. It cannot be done on one machine.
|
||||||
|
|
||||||
|
The stateless trick is to carry the state in the packet itself. zingela encodes the identity of each probe into the TCP initial sequence number using a keyed hash. When a reply comes back, the acknowledgement number carries that identity back, and a single hash recompute proves the reply is genuinely a response to a probe zingela sent. No table, no timers, no per-target memory. The transmit side and the receive side share almost nothing and run flat out.
|
||||||
|
|
||||||
|
This is the same design that let zmap scan the entire public IPv4 space in about 45 minutes from a single machine in 2013, a result that turned internet-wide scanning from a research curiosity into an everyday measurement tool.
|
||||||
|
|
||||||
|
## The honest positioning
|
||||||
|
|
||||||
|
Read this before you believe any speed claim, from us or anyone else.
|
||||||
|
|
||||||
|
On stock Linux hardware, masscan, zmap, and zingela all hit the same wall: the kernel `AF_PACKET` transmit path tops out around 1.5 to 2.5 million packets per second on a single core. Every one of these tools meets that ceiling. There is no raw-throughput bragging number to win on identical hardware, and zingela does not claim one.
|
||||||
|
|
||||||
|
The only road past that ceiling in masscan is proprietary: PF_RING ZC, which needs a paid ntop license, an out-of-tree kernel module, and specific Intel NICs. Its headline figures (10 Mpps on the README, higher in dual-NIC demos) are all PF_RING, never a stock kernel. zingela's road is `AF_XDP`, mainline in Linux since kernel 4.18, with no license and no proprietary module. That backend ships today behind the `-Dxdp` build flag. The full head-to-head against masscan on 10GbE hardware is future work, and these docs will never quote a line rate that has not been measured on real hardware.
|
||||||
|
|
||||||
|
So the defensible win is not "faster." It is: acceleration with no paywall, memory safety proven under Zig's ReleaseSafe checks, a single static binary, correctness that masscan never cleanly solved, and a modern interface. The [performance section of MECHANICS](MECHANICS.md) shows the measured hot-path numbers and explains why the CPU is never the bottleneck.
|
||||||
|
|
||||||
|
## What works today
|
||||||
|
|
||||||
|
- TCP SYN scan over IPv4 and IPv6, using a raw kernel-bypass transmit ring.
|
||||||
|
- UDP scan with per-protocol payloads and honest open-versus-filtered reporting.
|
||||||
|
- A TCP connect scan that needs no privileges, for locked-down or cloud environments.
|
||||||
|
- Automatic detection of hypervisors that silently drop raw sends, with a fallback to connect mode.
|
||||||
|
- Service and banner detection on open ports, opt-in and two-phase.
|
||||||
|
- A stealth and evasion suite, gated behind an explicit authorization acknowledgement.
|
||||||
|
- SipHash cookie statelessness, cyclic-group address permutation, token-bucket rate control, and a non-overridable exclusion of reserved address ranges.
|
||||||
|
- A truecolor live dashboard and greppable NDJSON output.
|
||||||
|
|
||||||
|
## Quick start
|
||||||
|
|
||||||
|
```
|
||||||
|
curl -fsSL https://angelamos.com/zingela/install.sh | bash
|
||||||
|
zingela scan --target 192.0.2.0/24 --ports 80,443 --rate 20000
|
||||||
|
```
|
||||||
|
|
||||||
|
The installer lands `zingela` on your PATH and grants the raw-socket capabilities so it runs without sudo. If you are on a box where you cannot or should not send raw packets, add `--connect` for the unprivileged scanner.
|
||||||
|
|
||||||
|
The target shown, `192.0.2.0/24`, is a reserved documentation range (RFC 5737) that zingela skips by design, so it reports zero targets. Substitute a range you own or are authorized to scan.
|
||||||
|
|
||||||
|
## Prerequisites for reading
|
||||||
|
|
||||||
|
You will get the most from these docs if you are comfortable with the TCP three-way handshake, the shape of IPv4 and TCP headers, and basic modular arithmetic. You do not need to know Zig; the walkthroughs name functions and files, not language trivia. Where a concept has a famous real-world example, we anchor it there.
|
||||||
|
|
||||||
|
## The rest of this folder
|
||||||
|
|
||||||
|
| Doc | What it covers |
|
||||||
|
|---|---|
|
||||||
|
| [01-CONCEPTS.md](01-CONCEPTS.md) | the security theory: statelessness, SYN cookies, what a scan can observe, responsible scanning, and the incidents that shaped all of it |
|
||||||
|
| [02-ARCHITECTURE.md](02-ARCHITECTURE.md) | the system design: the two-engine model, the module map, and the backend ladder, with diagrams |
|
||||||
|
| [03-IMPLEMENTATION.md](03-IMPLEMENTATION.md) | a guided walk through the code, module by module |
|
||||||
|
| [MECHANICS.md](MECHANICS.md) | the byte-level and math-level deep dive: the cookie, the checksum, the cyclic group, and the measured performance |
|
||||||
|
| [04-CHALLENGES.md](04-CHALLENGES.md) | ways to extend zingela, from a real AF_XDP benchmark to new probe modules |
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- 01-CONCEPTS.md -->
|
||||||
|
|
||||||
|
# Concepts
|
||||||
|
|
||||||
|
This doc explains the ideas zingela is built on, and grounds each in a real event so the stakes are concrete.
|
||||||
|
|
||||||
|
## The three-way handshake, and what a probe can learn
|
||||||
|
|
||||||
|
A TCP connection opens with three packets. The client sends a SYN. If the port is open, the server answers with a SYN-ACK. The client completes the connection with an ACK. If the port is closed, the server answers the SYN with a RST instead. If a firewall is dropping the traffic, nothing comes back at all.
|
||||||
|
|
||||||
|
A stateless SYN scan sends only the first packet and reads the answer:
|
||||||
|
|
||||||
|
```
|
||||||
|
zingela target
|
||||||
|
| ----- SYN --------> | open -> SYN-ACK comes back
|
||||||
|
| ----- SYN --------> | closed -> RST comes back
|
||||||
|
| ----- SYN --------> | filtered -> silence
|
||||||
|
```
|
||||||
|
|
||||||
|
zingela never sends the final ACK, so the connection is never established and the target application never sees a completed session. This is why a SYN scan is fast and light. It is also why the classification has exactly three outcomes: open (SYN-ACK), closed (RST), and filtered (no reply within the wait window). UDP is murkier, and gets its own honest treatment below.
|
||||||
|
|
||||||
|
## Statelessness: carry the state in the packet
|
||||||
|
|
||||||
|
The central idea. A stateful scanner remembers every probe it sent so it can match a reply. A stateless scanner remembers nothing and instead makes each reply prove its own legitimacy.
|
||||||
|
|
||||||
|
zingela does this with a keyed hash called a SYN cookie. Before sending a probe to a target, it computes a value from the four-tuple (source address, source port, destination address, destination port) using SipHash keyed with per-scan random entropy, and writes the low 32 bits into the TCP sequence number field. A real target that answers a SYN echoes that sequence number plus one in the acknowledgement field of its SYN-ACK. When the reply arrives, zingela recomputes the same hash from the packet's addresses and ports and checks that the acknowledgement equals the cookie plus one. If it matches, the reply is genuine. If it does not, the packet is a stray, a spoof, or a stale echo, and it is dropped for free.
|
||||||
|
|
||||||
|
The payoff is enormous. There is no connection table, so memory use is constant no matter how many billions of targets you sweep. The transmit engine and receive engine share no per-probe state, so they run as independent threads at full speed. Spoofed replies are rejected by arithmetic rather than by a firewall rule.
|
||||||
|
|
||||||
|
The cost is subtle and worth understanding. Because the scanner holds no record of what it sent, it cannot know a probe was lost, so loss looks identical to a filtered port. Serious stateless scanners answer this with unconditional retransmission (send every probe a few times) rather than with response tracking, because tracking would reintroduce the state you worked so hard to remove.
|
||||||
|
|
||||||
|
## SYN cookies came from a real attack
|
||||||
|
|
||||||
|
The cookie idea is not a scanner invention. It was Daniel Bernstein's 1996 defense against SYN flood denial-of-service attacks, in which an attacker sends floods of SYNs and never completes the handshake, exhausting the server's half-open connection table. Bernstein's answer was to stop storing half-open connections and instead encode the needed state in the sequence number itself, validating it when the ACK returns. A stateless scanner runs that same trick from the other side of the wire: the scanner, not the server, is the party that refuses to hold state.
|
||||||
|
|
||||||
|
## Walking the address space without a list
|
||||||
|
|
||||||
|
To scan a range you must visit every address in it, ideally in a scrambled order so you do not hammer one subnet at a time and trip its intrusion detection. The naive way is to build a list, shuffle it, and iterate. At internet scale the list alone is tens of gigabytes.
|
||||||
|
|
||||||
|
zingela borrows zmap's answer: a multiplicative cyclic group over a prime just above the size of the target space. It keeps three numbers (a current value, a generator, and the prime) and advances with one multiply and one modulo per target. Because the generator is a primitive root of the prime, the sequence visits every element of the space exactly once before returning to the start. It is a perfect shuffle with O(1) memory: no list, no bitmap of remaining work, no per-address random number call.
|
||||||
|
|
||||||
|
masscan solves the same problem with a block cipher (a Feistel network it calls BlackRock2). The 2024 "Ten Years of ZMap" retrospective measured the consequence: masscan finds notably fewer hosts than zmap, likely because of biases in that randomization. zingela takes the cyclic-group road precisely to avoid that bias. The [cyclic group section of MECHANICS](MECHANICS.md) shows the math.
|
||||||
|
|
||||||
|
## Rate limiting, and why the default is slow
|
||||||
|
|
||||||
|
zingela defaults to 10,000 packets per second, and it warns loudly before you push into the millions. That conservatism is deliberate, and it is a lesson written in outages.
|
||||||
|
|
||||||
|
On 25 January 2003 the SQL Slammer worm (exploiting CVE-2002-0649 in Microsoft SQL Server on UDP port 1434) demonstrated stateless scanning at its most destructive. It was a single 376-byte UDP packet that, on infecting a host, immediately began blasting copies to random addresses as fast as the network card allowed. It infected around 75,000 hosts in ten minutes, doubling roughly every 8.5 seconds, and the sheer volume of scan traffic saturated links and knocked parts of the internet offline. Slammer held no state and rate-limited nothing. It is the cautionary twin of every stateless scanner.
|
||||||
|
|
||||||
|
The lesson is that a tool which can emit packets at line rate is a tool that can flood a network by accident. masscan's own aggressive defaults have gotten it null-routed by ISPs. zingela uses a token bucket to pace transmission and makes fast scanning an explicit, warned choice rather than the default.
|
||||||
|
|
||||||
|
## Reserved ranges are excluded by construction
|
||||||
|
|
||||||
|
Some address blocks must never be scanned: loopback, private RFC 1918 space, link-local, multicast, and the documentation ranges. zingela checks a reserved-range table before it crafts any packet, and that exclusion floor cannot be overridden by a flag. If you point it at a range that overlaps reserved space, the reserved portion is subtracted and simply never probed. This is why aiming the scanner at `10.0.0.0/8` yields zero targets: the entire block is RFC 1918 private space and is removed before the first packet.
|
||||||
|
|
||||||
|
## Source spoofing is gated, not default
|
||||||
|
|
||||||
|
You can forge the source IP of a scan. Doing it by default would be reckless, and it barely works: only about a fifth of autonomous systems on the internet permit spoofed egress, because most implement the anti-spoofing filtering described in BCP 38. The legitimate use is testing your own network's BCP 38 compliance. zingela therefore treats spoofing and every other evasion feature as gated behind an explicit `--authorized-scan` acknowledgement, never as a quiet default.
|
||||||
|
|
||||||
|
## The legal line
|
||||||
|
|
||||||
|
Scanning sends unsolicited packets to machines you may not own. In the United States that can fall under the Computer Fraud and Abuse Act, the same 1986 statute under which Robert Tappan Morris was convicted after his 1988 worm scanned and infected roughly 6,000 machines, about a tenth of the internet of the day. Courts have treated unauthorized scanning inconsistently, but the safe rule is simple and absolute: scan only systems you own or have explicit written permission to test.
|
||||||
|
|
||||||
|
The same techniques serve defense. When Heartbleed (CVE-2014-0160) broke in April 2014, researchers used zmap to scan the entire internet repeatedly, measuring how many servers were vulnerable and how fast operators patched. Internet-wide measurement, vulnerability tracking, and asset inventory are exactly what these tools are for, when you have the authority to run them. zingela's defaults, its gating, and its exclusion floor are there to keep the tool on the right side of that line.
|
||||||
|
|
||||||
|
## Why aggressive scanning gets you noticed
|
||||||
|
|
||||||
|
The 2016 Mirai botnet scanned TCP ports 23 and 2323 across the entire IPv4 space, trying about 60 default telnet credentials, and assembled a botnet large enough to take down Krebs on Security, OVH, and the Dyn DNS provider in October 2016. It was loud, fast, and indiscriminate, and it was trivially detectable precisely because of that. The stealth suite in zingela exists so that authorized testers can study detectability (jitter, decoys, OS-realistic fingerprints, alternate scan types), not so that anyone can be quieter while doing harm. That is why it is gated. See [04-CHALLENGES.md](04-CHALLENGES.md) for the evasion topics as study exercises.
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- 02-ARCHITECTURE.md -->
|
||||||
|
|
||||||
|
# Architecture
|
||||||
|
|
||||||
|
zingela is two machines bolted together: a stateless line-rate transmit and receive core that owns the packet hot path, and a memory-safe control plane that parses your intent, paces the sending, and presents the results. This doc shows how they fit.
|
||||||
|
|
||||||
|
## The big picture
|
||||||
|
|
||||||
|
```
|
||||||
|
you control plane data plane
|
||||||
|
------------ ------------------------------------ --------------------
|
||||||
|
--target -> cli.zig -> scancmd.zig tx.zig (transmit)
|
||||||
|
--ports parse validate, build Config -> pull target
|
||||||
|
--rate help select backend stamp frame
|
||||||
|
--backend banner launch engines rate-gate
|
||||||
|
| \ push to ring
|
||||||
|
| \ |
|
||||||
|
v v v
|
||||||
|
output.zig rx.zig (receive) packet_io.zig
|
||||||
|
dashboard read frame AF_XDP or
|
||||||
|
table validate cookie AF_PACKET or
|
||||||
|
NDJSON classify connect
|
||||||
|
^ dedup
|
||||||
|
| push found host
|
||||||
|
+---------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
The control plane never touches a raw socket in the hot loop. The data plane never allocates after startup. That separation is what keeps the scanner both safe and fast.
|
||||||
|
|
||||||
|
## The stateless data flow
|
||||||
|
|
||||||
|
Follow one probe and its reply all the way through. This is the heart of the design.
|
||||||
|
|
||||||
|
```
|
||||||
|
TRANSMIT RECEIVE
|
||||||
|
-------- -------
|
||||||
|
targets.Engine.next() packet_io rxPoll()
|
||||||
|
gives (ip, port) reads a frame
|
||||||
|
| |
|
||||||
|
v v
|
||||||
|
cookie.seq(ip,port,src,srcport) classify.classifyTcp()
|
||||||
|
= low 32 bits of is it SYN-ACK / RST / ICMP?
|
||||||
|
SipHash(4-tuple) |
|
||||||
|
| v
|
||||||
|
v cookie.validateSynAck(ack,...)
|
||||||
|
template.stamp(frame, ip, port) ack == cookie + 1 ?
|
||||||
|
writes Eth+IP+TCP, |
|
||||||
|
puts cookie in TCP seq yes --+-- no
|
||||||
|
| | |
|
||||||
|
v v v
|
||||||
|
ratelimit token gate dedup.insert drop (stray/spoof)
|
||||||
|
| new host?
|
||||||
|
v |
|
||||||
|
packet_io txSubmit + txKick --- wire ---> output emit
|
||||||
|
```
|
||||||
|
|
||||||
|
Nothing links the left column to the right except arithmetic. The cookie written into the sequence number on transmit is the same cookie recomputed and checked on receive. There is no shared table, no correlation ID lookup, no per-probe memory. A reply that does not carry a valid cookie is discarded before it can pollute the results.
|
||||||
|
|
||||||
|
## The two engines
|
||||||
|
|
||||||
|
The transmit engine and the receive engine run as concurrent tasks launched with `io.concurrent` over Zig's `std.Io.Threaded`. This is not the old `async` keyword, which Zig removed, and it is not a future per probe. It is two long-lived workers.
|
||||||
|
|
||||||
|
The transmit engine runs hot. It pulls targets from the address permutation, stamps each into a preallocated frame buffer, passes it through the token-bucket rate gate, and hands it to the packet backend. After startup it does not allocate: the transmit ring is memory-mapped once, and per-packet scratch comes from a fixed buffer.
|
||||||
|
|
||||||
|
The receive engine reads frames from the backend, classifies each as SYN-ACK, RST, or a relevant ICMP error, validates the cookie, deduplicates against a lockless open-addressed hash table so a host that answers twice is reported once, and pushes newly found hosts to the output side. Replies are sparse compared to probes, so the receive engine is far less hot than the transmit engine, which is why the backend design below can afford to be asymmetric.
|
||||||
|
|
||||||
|
## The backend ladder
|
||||||
|
|
||||||
|
The packet input and output path is chosen at runtime through one interface (`packet_io.zig`) with several implementations. The scanner core does not know or care which one is active.
|
||||||
|
|
||||||
|
```
|
||||||
|
--backend auto picks the first that works:
|
||||||
|
|
||||||
|
1. AF_XDP zero-copy fastest, driver-dependent -Dxdp build
|
||||||
|
2. AF_XDP with XDP_SKB copy mode, works on more drivers -Dxdp build
|
||||||
|
3. AF_PACKET + TX_RING universal, kernel-bypass TX ring always built
|
||||||
|
+ QDISC_BYPASS the v1 default, matches masscan-stock
|
||||||
|
4. AF_PACKET sendto plain, used by the ground-truth smoke
|
||||||
|
5. connect scan TCP via the OS stack, no raw needed --connect
|
||||||
|
```
|
||||||
|
|
||||||
|
The AF_XDP path is asymmetric: it accelerates transmit and pairs with an `AF_PACKET` receive path. Replies are sparse, so this trades a few percent of theoretical receive throughput for a much simpler design with no receive-side steering or map wiring. When `--backend auto` runs, a two-socket self-probe first checks whether raw sends actually leave the machine. On a cloud instance or a VM where the hypervisor silently drops raw frames, that probe fails and zingela falls back to the connect scanner with a clear notice, rather than reporting a scan that sent nothing.
|
||||||
|
|
||||||
|
## The module map
|
||||||
|
|
||||||
|
| Module | Responsibility |
|
||||||
|
|---|---|
|
||||||
|
| `main.zig` | entry point: parse arguments, build config, dispatch to a subcommand |
|
||||||
|
| `cli.zig` | argument parsing, help, the banner and version |
|
||||||
|
| `scancmd.zig` | the scan command: validate, select backend, launch transmit and receive, drive output |
|
||||||
|
| `txcmd.zig` | the transmit-only blast command |
|
||||||
|
| `targets.zig` | the cyclic-group permutation engine, primitive-root finder, address and port picker, exclusion floor |
|
||||||
|
| `numtheory.zig` | modular exponentiation, primality, primitive-root testing for the permutation |
|
||||||
|
| `packet.zig` | wire-format headers, the RFC 1071 checksum (scalar and SIMD), OS-realistic SYN presets |
|
||||||
|
| `cookie.zig` | SipHash SYN-cookie generation and validation, IPv4 and IPv6 |
|
||||||
|
| `template.zig` | the SYN frame template that gets stamped per target |
|
||||||
|
| `ratelimit.zig` | the token-bucket pacer |
|
||||||
|
| `tx.zig` | the transmit engine |
|
||||||
|
| `rx.zig` | the receive engine |
|
||||||
|
| `classify.zig` | reply classification into open, closed, filtered |
|
||||||
|
| `dedup.zig` | the lockless open-addressed dedup table |
|
||||||
|
| `packet_io.zig` | the backend interface, with `afpacket.zig`, `afxdp.zig`, and `connect.zig` implementations |
|
||||||
|
| `udp.zig` | UDP payloads and UDP-specific classification |
|
||||||
|
| `service.zig` | the opt-in banner and service detection phase |
|
||||||
|
| `stealth.zig` | the gated evasion features |
|
||||||
|
| `output.zig` | the live dashboard, results table, and NDJSON |
|
||||||
|
| `netutil.zig`, `ndp.zig`, `rawprobe.zig` | interface and gateway resolution, IPv6 neighbor discovery, the cloud self-probe |
|
||||||
|
|
||||||
|
## Why the split matters for safety
|
||||||
|
|
||||||
|
The 2003 SQL Slammer worm and every buffer-overflow scanning worm since share a root cause: hand-written packet code in C that corrupts memory under load. zingela builds and tests under Zig's ReleaseSafe mode first, where an out-of-bounds slice, an integer overflow, or a null dereference is a defined trap rather than silent memory corruption. The wire-format structs carry compile-time size assertions, every checksum and cookie is checked against a published test vector, and the whole scan is validated end to end inside a network namespace before any release. The [implementation walkthrough](03-IMPLEMENTATION.md) shows where each of these guards lives.
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- 03-IMPLEMENTATION.md -->
|
||||||
|
|
||||||
|
# Implementation Walkthrough
|
||||||
|
|
||||||
|
A guided tour of the code. It names functions and files, never line numbers, so it stays correct as the source moves. Read it with the source open beside you. For the byte-level and math-level details, follow the pointers into [MECHANICS.md](MECHANICS.md).
|
||||||
|
|
||||||
|
## Wire formats and the checksum: `packet.zig`
|
||||||
|
|
||||||
|
Everything the scanner puts on the wire starts here. The Ethernet, IPv4, TCP, UDP, and IPv6 headers are declared as `extern struct` so their field order and packing match the wire exactly, and each carries a compile-time size assertion so a mistaken field can never silently change the layout. Multi-byte fields are converted to network byte order explicitly at the point of writing, never left to chance.
|
||||||
|
|
||||||
|
The checksum lives in two functions. `checksum` is the scalar one's-complement sum from RFC 1071. `checksumSimd` is the same math over a vector accumulator for large buffers. Both are validated against the published RFC 1071 worked example. `tcpChecksum` and `tcpChecksum6` build the pseudo-header and run the sum over the TCP segment for IPv4 and IPv6. There is also `incrementalUpdate`, the RFC 1624 trick that patches a checksum when a single field changes without resumming the whole packet.
|
||||||
|
|
||||||
|
`ScanType` and `OsProfile` encode the stealth variations. `ScanType.probeFlags` returns the TCP flag byte for a syn, fin, null, xmas, maimon, ack, or window scan, and `OsProfile.options` returns the TCP option bytes of a realistic Linux, Windows, or macOS SYN so a fingerprint looks like a real client rather than a scanner.
|
||||||
|
|
||||||
|
The `Addr` union is how one result path serves both address families: it holds either a `u32` for IPv4 or a `[16]u8` for IPv6, so the classification, output, and dedup code is written once.
|
||||||
|
|
||||||
|
## The cookie: `cookie.zig`
|
||||||
|
|
||||||
|
This is statelessness in about sixty lines. A `Cookie` is a 16-byte key, created from per-scan random entropy. `generate` packs the four-tuple into a small buffer and returns `std.hash.SipHash64(2, 4).toInt` over it, a full 64-bit hash. `seq` truncates that to the 32 bits that fit in a TCP sequence number. `validateSynAck` recomputes `seq` from the reply's addresses and ports and returns whether the acknowledgement equals `seq +% 1`, using wrapping addition because the cookie can be `0xFFFFFFFF` and a plain add would overflow-trap in a safe build.
|
||||||
|
|
||||||
|
The IPv6 variants `generate6`, `seq6`, and `validateSynAck6` do the same over 16-byte addresses. `udpSrcPort` reuses the hash to derive a per-target source port for UDP, so the reply's destination port itself carries validating information. A unit test reproduces the canonical SipHash empty-message vector, so the primitive is proven, not assumed.
|
||||||
|
|
||||||
|
## The address engine: `targets.zig` and `numtheory.zig`
|
||||||
|
|
||||||
|
`parseCidr` turns `192.0.2.0/24` into a `Range` of start and end addresses. `IpPicker.build` takes your ranges, subtracts every reserved block through `subtractReserved`, sorts what remains, and builds a cumulative-prefix index so that a flat integer can be mapped to a concrete address with a binary search in `IpPicker.at`. This is where `10.0.0.0/8` collapses to nothing: the reserved subtraction removes the whole private block.
|
||||||
|
|
||||||
|
`Engine` is the permutation. `Engine.init` computes the total size of the address-and-port space, finds the smallest prime above it with `numtheory.smallestPrimeAbove`, and picks a fresh primitive root with `numtheory.findPrimitiveRoot`. Note the difference from zmap here: zmap hardcodes a table of eleven prime and primitive-root pairs, while zingela computes the prime for the exact size of your scan at runtime, so a small scan gets a small group and wastes no iterations re-rolling out-of-range values. `Engine.next` advances the group with `numtheory.mulMod`, decodes the resulting element into an address and port through the picker, and returns one `Target`. `initShard` slices the group into contiguous arcs so several machines can share one scan by seed.
|
||||||
|
|
||||||
|
`numtheory.findPrimitiveRoot` is the interesting one. Rather than brute-force testing, it factors `prime - 1` with `distinctPrimeFactors` and, for each random candidate, checks with `isPrimitiveRoot` that no `modExp(candidate, (prime-1)/q, prime)` equals one. A candidate that passes generates the whole group. The math is in [MECHANICS.md](MECHANICS.md).
|
||||||
|
|
||||||
|
## Stamping frames: `template.zig`
|
||||||
|
|
||||||
|
`SynTemplate.init` builds a base frame once from a `Config` (source and destination MAC, source address and port, cookie, OS profile, scan type), laying down the Ethernet, IPv4, and TCP headers with everything that does not change between targets. `stamp` is the per-target hot function: it copies the base, writes the destination address, varies the IP identification field, writes the destination port, computes the cookie with `cookie.seq` and places it in the sequence or acknowledgement field depending on the scan type, and finishes with the IP and TCP checksums. `stampVariant` layers decoy source addresses on top for the stealth suite. `SynTemplate6` is the IPv6 counterpart.
|
||||||
|
|
||||||
|
## Pacing: `ratelimit.zig`
|
||||||
|
|
||||||
|
`TokenBucket` is a nanosecond bank. `init` sets a per-token step in nanoseconds from your requested packets per second and a capacity for short bursts. `takeBatch` reads the monotonic clock, credits elapsed time into the bank, and returns how many tokens the transmit engine may spend right now, which is how the sender stays at rate without busy-waiting. `refund` returns tokens when a send could not be completed, and `withJitter` perturbs the timing for the Poisson jitter mode. This is a genuine token bucket, distinct from masscan's proportional-feedback controller over a timestamp ring.
|
||||||
|
|
||||||
|
## The engines: `tx.zig` and `rx.zig`
|
||||||
|
|
||||||
|
`tx.zig` is the transmit loop: prime the bucket, pull targets from the `Engine`, stamp them through the template, gate on the bucket, and batch them into the backend's transmit ring before kicking the kernel to send. It allocates nothing after startup. `rx.zig` opens the receive socket for the right ethertype and reads frames in a loop.
|
||||||
|
|
||||||
|
Classification lives in `classify.zig`. `classifyTcp` inspects a received TCP segment and decides open, closed, or filtered, gated on a valid cookie so that only genuine replies count, and maps relevant ICMP errors to filtered. `classifyTcp6` does the same for IPv6, including ICMPv6 unreachables. Deduplication lives in `dedup.zig`: `Dedup.insert` mixes the key with an fmix64 finalizer and places it in an open-addressed table with linear probing, returning whether the host was newly seen so each host is reported once.
|
||||||
|
|
||||||
|
## The backends: `packet_io.zig`, `afpacket.zig`, `afxdp.zig`, `connect.zig`
|
||||||
|
|
||||||
|
`packet_io.zig` defines the interface the engines call: open, reserve a transmit slot, submit, kick, poll for received frames, close. `afpacket.zig` implements it over an `AF_PACKET` socket with a memory-mapped `PACKET_TX_RING` and `PACKET_QDISC_BYPASS`, the universal path that needs no kernel modules. `afxdp.zig` implements the accelerated path over `AF_XDP` with its own user-memory region and rings, built only when `-Dxdp` is set. `connect.zig` is the unprivileged fallback: it makes raw non-blocking `connect` calls through `std.os.linux`, waits on them with `poll`, and reads the socket error to classify open, closed, or filtered. It has its own sized thread pool because a connect blocks a thread, so concurrency equals pool size.
|
||||||
|
|
||||||
|
This raw-socket approach is deliberate. The connect scanner drives non-blocking sockets and `poll` itself rather than going through the standard networking layer's connect-with-timeout, which `connect.zig` does not use for the scan path.
|
||||||
|
|
||||||
|
## UDP, service detection, stealth, and output
|
||||||
|
|
||||||
|
`udp.zig` holds the compile-time payload table and the UDP classification, where an ICMP type-3 code-3 unreachable means closed and silence is reported honestly as open-or-filtered because a UDP service may simply not answer a probe it does not recognize. `service.zig` is the opt-in second phase: on an open port it sends a NULL probe and an HTTP request, grabs the banner, and detects TLS without decrypting it. `stealth.zig` gathers the gated evasion features. `output.zig` renders the truecolor live dashboard and the results table to the terminal while writing NDJSON to standard output, so the visual and machine views never collide.
|
||||||
|
|
||||||
|
## Where the proofs live
|
||||||
|
|
||||||
|
Every one of these modules ships unit tests in the same file, run with `zig build test`. The checksum has its RFC vector, the cookie has its SipHash vector and a cookie-plus-one round trip, the address engine has a bijection property test over a small space, and the dedup table has growth and collision tests. The whole pipeline is exercised end to end inside a network namespace before release. That is the repository rule: no code without proof.
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- 04-CHALLENGES.md -->
|
||||||
|
|
||||||
|
# Challenges and Extensions
|
||||||
|
|
||||||
|
Ways to take zingela further, ordered roughly from approachable to deep. Each names the code it touches and the idea it teaches. Some are genuine gaps in the current build, marked as such. Scan only what you are authorized to scan while testing any of these.
|
||||||
|
|
||||||
|
## Warm up
|
||||||
|
|
||||||
|
### Add a probe payload
|
||||||
|
|
||||||
|
`udp.zig` holds a compile-time table of per-protocol UDP payloads (the bytes that make a DNS, NTP, or SNMP service answer). Add one for a protocol it does not cover yet, for example a memcached stats request or an IKE handshake. You will learn why UDP scanning needs a real payload: unlike TCP, a bare UDP probe to an open port usually gets no reply, so silence is honestly reported as open-or-filtered. A good payload turns some of those into definite opens. This is the same reason the 2003 SQL Slammer worm fit its whole exploit in one 376-byte UDP packet to port 1434: UDP services answer content, not connection attempts.
|
||||||
|
|
||||||
|
### Extend service detection
|
||||||
|
|
||||||
|
`service.zig` runs a NULL probe and an HTTP request on open ports and grabs the banner. Add a probe for another common service, say an SSH version string or a TLS ClientHello that records the certificate. Note the boundary the project holds deliberately: it detects TLS but does not compute a JA4 fingerprint, because a dedicated Rust tool already does that. Respect that boundary or you are duplicating work.
|
||||||
|
|
||||||
|
## Build something real
|
||||||
|
|
||||||
|
### A real AF_XDP benchmark
|
||||||
|
|
||||||
|
This is the honest open question of the whole project. The `AF_XDP` transmit backend ships behind `-Dxdp`, but the head-to-head against masscan on 10GbE hardware has not been measured. Set up two machines with capable NICs, run zingela with `-Dxdp` against a sink, measure sustained packets per second with `perf`, and compare to masscan on the same hardware with and without PF_RING. Publish the real number. The Zippier ZMap paper measured zmap at 14.23 Mpps versus masscan at 6.4 Mpps on 10G, so there is a concrete target to reproduce and beat. Do not accept a veth or loopback figure as a line rate; those measure the kernel loopback, not the NIC.
|
||||||
|
|
||||||
|
### Rate warnings and a hard gate
|
||||||
|
|
||||||
|
The design calls for a prominent warning above one million packets per second and an explicit confirmation gate above ten million, so that fast scanning is always a deliberate choice. Wire that into `ratelimit.zig` and the argument parser. The reason is written in outages: masscan's aggressive defaults have gotten it null-routed by ISPs, and Slammer showed what an unpaced stateless sender does to a network. This teaches you where responsibility lives in a tool that can flood a link by accident.
|
||||||
|
|
||||||
|
### An atomic resume checkpoint
|
||||||
|
|
||||||
|
A long scan that dies halfway should be resumable. Because the address engine is a cyclic group with O(1) state (a current value, a generator, a prime, and a step count), the entire progress of a scan is a handful of integers. Periodically write them to disk atomically, and add a flag to resume from that file. This teaches why the stateless permutation is not just fast but operationally convenient: you can checkpoint an internet-wide scan in a few bytes, which a shuffled address list could never do.
|
||||||
|
|
||||||
|
## Go deep
|
||||||
|
|
||||||
|
### IPv6 UDP and IPv6 stealth
|
||||||
|
|
||||||
|
The current IPv6 support is TCP SYN only. UDP scanning and the stealth suite are IPv4 only. Extending them to IPv6 means reworking the UDP payload path and the evasion features to carry `packet.Addr` all the way through, the way the TCP path already does. The dedup key for IPv6 is a lossy hash of the 144-bit address-and-port down to 64 bits, so think about whether your extension needs an exact key. This is a real architecture exercise in how far a union type carries you.
|
||||||
|
|
||||||
|
### Embedded IPv4-in-IPv6 literals
|
||||||
|
|
||||||
|
Addresses like `::ffff:1.2.3.4` embed an IPv4 address in IPv6 notation. The parser in `netutil.zig` does not accept them yet. Adding it is a small, well-scoped parsing task with sharp edge cases, a good way to learn the IPv6 text format and to practice property-testing a parser against its inverse.
|
||||||
|
|
||||||
|
### Sharding across machines
|
||||||
|
|
||||||
|
`Engine.initShard` already slices the cyclic group into contiguous arcs by shard identifier and count, so several machines sharing one seed can each scan a disjoint slice of the same space with no coordination. Build the orchestration around it: a launcher that starts N shards, collects their NDJSON, and merges the results. This is how internet-scale scans are actually run, and it teaches why a seeded deterministic permutation is the right primitive for distribution.
|
||||||
|
|
||||||
|
### A results store and a detection view
|
||||||
|
|
||||||
|
zingela emits NDJSON to standard output. Feed it into a store (SQLite, or a columnar file) and build queries over it: new hosts since the last scan, services that changed, ports that opened. This is the measurement use case that internet scanning exists for. When Heartbleed broke in 2014, researchers scanned the whole internet repeatedly with zmap precisely to track which servers stayed vulnerable over time. A results store is what turns a scanner into a measurement instrument.
|
||||||
|
|
||||||
|
## Study, do not weaponize
|
||||||
|
|
||||||
|
### Detectability experiments
|
||||||
|
|
||||||
|
The stealth suite (jitter, decoys, OS-realistic fingerprints, alternate scan types), gated behind `--authorized-scan`, exists so that authorized testers can study how a scan looks to an intrusion detection system. Stand up a lab with your own IDS, scan your own targets, and measure which techniques change the alert profile and which do not. The 2016 Mirai botnet was caught in part because its scanning was loud and uniform. Understanding detectability is defensive knowledge. Using it to scan systems you do not own is not, and the gating is there to keep that distinction sharp.
|
||||||
|
|
@ -0,0 +1,137 @@
|
||||||
|
<!-- ©AngelaMos | 2026 -->
|
||||||
|
<!-- MECHANICS.md -->
|
||||||
|
|
||||||
|
# Mechanics
|
||||||
|
|
||||||
|
The byte-level and math-level deep dive. Where [03-IMPLEMENTATION.md](03-IMPLEMENTATION.md) says what each module does, this doc shows the arithmetic that makes it correct and fast. Everything here matches the source in `cookie.zig`, `packet.zig`, `targets.zig`, `numtheory.zig`, `dedup.zig`, and `ratelimit.zig`.
|
||||||
|
|
||||||
|
## The SYN cookie, byte by byte
|
||||||
|
|
||||||
|
The cookie is what lets zingela hold no state. `generate` builds a 12-byte message from the four-tuple, in network byte order, and hashes it:
|
||||||
|
|
||||||
|
```
|
||||||
|
data[12]:
|
||||||
|
0 4 6 10 12
|
||||||
|
+--------+------+------------+----+
|
||||||
|
| ip_them|p_them| ip_me |p_me|
|
||||||
|
+--------+------+------------+----+
|
||||||
|
u32 BE u16 BE u32 BE u16 BE
|
||||||
|
|
||||||
|
cookie64 = SipHash64(2,4)(data, key) key = 16 random bytes per scan
|
||||||
|
seq32 = low 32 bits of cookie64
|
||||||
|
```
|
||||||
|
|
||||||
|
SipHash is a keyed pseudo-random function. Without the key you cannot predict its output, so an attacker cannot forge a sequence number that will validate. The `(2, 4)` are the compression and finalization round counts of the standard SipHash-2-4. A unit test in `cookie.zig` reproduces the published SipHash empty-message vector, which proves the primitive is wired correctly.
|
||||||
|
|
||||||
|
On transmit, `seq32` goes into the TCP sequence field. A target that accepts the SYN answers with a SYN-ACK whose acknowledgement field is your sequence number plus one, because TCP acknowledges the next byte it expects. On receive, `validateSynAck` recomputes `seq32` from the reply's own addresses and ports and checks:
|
||||||
|
|
||||||
|
```
|
||||||
|
ack == seq32 +% 1
|
||||||
|
```
|
||||||
|
|
||||||
|
The `+%` is wrapping addition. It matters: `seq32` can be `0xFFFFFFFF`, and in a safe Zig build a plain `+ 1` on that would trap on overflow. Wrapping makes `0xFFFFFFFF + 1` become `0x00000000`, exactly as the target's TCP stack computes it.
|
||||||
|
|
||||||
|
Why this rejects spoofs. A packet that was not generated in response to one of our probes will, with overwhelming probability, carry an acknowledgement that does not equal the keyed hash of its own four-tuple plus one. An attacker would need the per-scan key to forge one. So validation is a single hash recompute and a compare, no table lookup, and it throws away strays, late duplicates, and injected spoofs for free.
|
||||||
|
|
||||||
|
For UDP, `udpSrcPort` derives the source port itself from the hash, so the reply's destination port carries the validating information even when the payload does not.
|
||||||
|
|
||||||
|
## The RFC 1071 checksum
|
||||||
|
|
||||||
|
The internet checksum is the one's-complement sum of 16-bit words. `checksum` in `packet.zig` implements it directly:
|
||||||
|
|
||||||
|
```
|
||||||
|
sum = 0
|
||||||
|
for each 16-bit big-endian word w in the buffer:
|
||||||
|
sum += w (sum is 32-bit, so carries pile up high)
|
||||||
|
if one odd byte remains:
|
||||||
|
sum += byte << 8 (pad on the right with zero)
|
||||||
|
while sum >> 16 != 0: (fold the carries back in)
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16)
|
||||||
|
return ~sum (one's complement, low 16 bits)
|
||||||
|
```
|
||||||
|
|
||||||
|
The fold loop is the end-around carry that defines one's-complement arithmetic: any overflow above bit 15 is added back into the low word. A worked feel for it: if the running sum reached `0x1_2345`, one fold gives `0x2345 + 0x1 = 0x2346`, and the returned checksum is `~0x2346 = 0xDCB9`. The receiver runs the identical sum including the checksum field and expects `0xFFFF`, which is how a corrupt packet is caught.
|
||||||
|
|
||||||
|
`checksumSimd` computes the same value faster on large buffers. It reads the buffer in blocks the width of the machine's vector unit, reinterprets each block as a vector of 16-bit words, byte-swaps them on a little-endian CPU so the arithmetic is big-endian, and accumulates into a vector of 32-bit lanes. After the vectorized body it reduces the lanes to one sum, handles any tail bytes with the scalar loop, and folds and complements exactly as above. The result is identical to the scalar function, which the tests assert.
|
||||||
|
|
||||||
|
There is a third function, `incrementalUpdate`, from RFC 1624. When exactly one 16-bit field of a packet changes, you do not need to resum the whole thing. Given the old checksum, the old word, and the new word:
|
||||||
|
|
||||||
|
```
|
||||||
|
sum = ~old_check + ~old_word + new_word
|
||||||
|
fold carries
|
||||||
|
return ~sum
|
||||||
|
```
|
||||||
|
|
||||||
|
This is why stamping a new destination address into a cached template does not cost a full checksum pass over every packet.
|
||||||
|
|
||||||
|
## The cyclic group permutation
|
||||||
|
|
||||||
|
The address engine visits every address and port exactly once, in scrambled order, with three numbers of state. The math is the multiplicative group of integers modulo a prime.
|
||||||
|
|
||||||
|
Pick a prime `p` just larger than the number of targets `N`. The set `{1, 2, ..., p-1}` under multiplication modulo `p` is a cyclic group of order `p-1`. A **primitive root** `g` is a generator of that group: the sequence
|
||||||
|
|
||||||
|
```
|
||||||
|
g^1, g^2, g^3, ... (mod p)
|
||||||
|
```
|
||||||
|
|
||||||
|
hits every one of the `p-1` nonzero residues exactly once before it repeats. So the engine keeps a current value and advances it:
|
||||||
|
|
||||||
|
```
|
||||||
|
current = (current * g) mod p one multiply, one modulo, per target
|
||||||
|
```
|
||||||
|
|
||||||
|
Each `current` is a distinct integer in `[1, p-1]`. If it happens to exceed `N` (there are only a handful of such values, since `p` is just above `N`), the engine simply advances again without emitting, which is the re-roll. Every value in `[1, N]` is produced exactly once, in an order that looks random because multiplication by a primitive root scrambles the sequence.
|
||||||
|
|
||||||
|
Encoding both address and port in one element is a division. For target index `idx` in `[1, N]`, let `idx0 = idx - 1`, then:
|
||||||
|
|
||||||
|
```
|
||||||
|
ip_position = idx0 / number_of_ports
|
||||||
|
port_position = idx0 % number_of_ports
|
||||||
|
```
|
||||||
|
|
||||||
|
so one traversal of the group randomizes the entire address-and-port space at once, not port by port.
|
||||||
|
|
||||||
|
Finding a primitive root without brute force is the clever part, in `numtheory.findPrimitiveRoot`. A candidate `g` generates the whole group if and only if, for every distinct prime factor `q` of `p-1`, `g^((p-1)/q) mod p` is not one. If any of those powers is one, `g` only generates a proper subgroup and is rejected. So the code factors `p-1` once with `distinctPrimeFactors`, then tests random candidates with `modExp` and `isPrimitiveRoot` until one passes. Each scan gets a fresh generator, so each scan is a different permutation.
|
||||||
|
|
||||||
|
zingela differs from zmap here in a way worth stating. zmap hardcodes eleven prime and primitive-root pairs, one per supported space size, the largest being `2^48 + 23`. zingela calls `smallestPrimeAbove(N)` for the exact size of your scan and finds a generator at runtime. A scan of a `/24` on one port uses a prime near 256, not a prime near `2^48`, so it wastes no iterations re-rolling values that fall outside a giant fixed group.
|
||||||
|
|
||||||
|
## The dedup table
|
||||||
|
|
||||||
|
A host that answers on two ports, or answers a retransmitted probe twice, must be counted once. `dedup.zig` is an open-addressed hash set of 64-bit keys with linear probing.
|
||||||
|
|
||||||
|
The key packs the address and port: `(ip << 16) | port`. Its maximum value is `0x0000FFFFFFFFFFFF`, which can never equal the empty-slot sentinel `0xFFFFFFFFFFFFFFFF`, so the sentinel is safe.
|
||||||
|
|
||||||
|
The hash is an fmix64 finalizer:
|
||||||
|
|
||||||
|
```
|
||||||
|
x ^= x >> 33
|
||||||
|
x *= 0xff51afd7ed558ccd
|
||||||
|
x ^= x >> 33
|
||||||
|
```
|
||||||
|
|
||||||
|
This spreads the low-entropy key across all 64 bits so that sequential addresses do not cluster into one probe chain. `insert` computes the slot from the hash masked to the table size, then probes forward linearly until it finds the key (a duplicate, return false) or an empty slot (insert, return true). The table grows and rehashes when it passes seven-tenths full, which keeps probe chains short. Because the table is owned by the single receive engine, it needs no locks.
|
||||||
|
|
||||||
|
## The token bucket
|
||||||
|
|
||||||
|
`ratelimit.zig` paces transmission with a nanosecond bank rather than a sleep. `init` computes a per-token cost `step_ns = 1_000_000_000 / rate` and a capacity for short bursts. `takeBatch` reads the monotonic clock, adds the elapsed nanoseconds since the last call into the bank up to the cap, and returns how many whole tokens (`bank / step_ns`) the sender may spend now, deducting them. The sender never busy-waits: it asks for a batch, sends that many, and asks again. `refund` returns tokens for sends that could not complete, and `withJitter` perturbs the step for Poisson timing. This is a true token bucket, which is a different mechanism from masscan's proportional-feedback controller over a 256-slot timestamp ring.
|
||||||
|
|
||||||
|
## Measured performance
|
||||||
|
|
||||||
|
These are the hot-path functions timed by `zig build bench` on an Intel Core i7-14700KF, single core, ReleaseFast. Each measurement varies its input every iteration and folds the result into a printed value so the optimizer cannot delete the work. Numbers scale with the CPU; run the bench on your own box.
|
||||||
|
|
||||||
|
| Operation | ns/op | Rate |
|
||||||
|
|---|---|---|
|
||||||
|
| RFC 1071 checksum, scalar, 40 B | 6.0 | 6.7 GB/s |
|
||||||
|
| RFC 1071 checksum, scalar, 1500 B | 185 | 8.1 GB/s |
|
||||||
|
| RFC 1071 checksum, SIMD, 1500 B | 30.4 | 49 GB/s |
|
||||||
|
| SipHash cookie generate | 14.0 | 71 M/s |
|
||||||
|
| SipHash cookie validate | 14.8 | 68 M/s |
|
||||||
|
| SYN frame stamp, full frame | 22.9 | 44 M/s |
|
||||||
|
| Cyclic-group permutation step | 4.6 | 216 M/s |
|
||||||
|
| Dedup insert | 15.4 | 65 M/s |
|
||||||
|
|
||||||
|
Two things are worth reading out of this table.
|
||||||
|
|
||||||
|
First, the frame-stamp rate against the ceiling. A single core stamps about 44 million complete SYN frames per second. The `AF_PACKET` kernel transmit path drains at 1.5 to 2.5 million packets per second. So the CPU builds frames roughly twenty times faster than the kernel can send them. The scanner is never CPU-bound on stock hardware. This is the whole basis of the honest positioning: since every scanner meets the same kernel ceiling, there is no raw-throughput win to claim there, and the only real path to more packets per second is bypassing the kernel with `AF_XDP`.
|
||||||
|
|
||||||
|
Second, the SIMD checksum is faster than scalar only on large buffers. On a 40-byte header the vector setup does not pay for itself, so scalar wins. On a 1500-byte frame the vector version is about six times faster. The scanner stamps small headers, so `template.stamp` uses the scalar `checksum`, and the benchmark makes that choice visible rather than assumed.
|
||||||
|
|
@ -0,0 +1,221 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// afpacket.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
const TPACKET_ALIGNMENT: usize = 16;
|
||||||
|
|
||||||
|
fn tpacketAlign(x: usize) usize {
|
||||||
|
return (x + TPACKET_ALIGNMENT - 1) & ~(TPACKET_ALIGNMENT - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const tpacket2_hdr = extern struct {
|
||||||
|
tp_status: u32,
|
||||||
|
tp_len: u32,
|
||||||
|
tp_snaplen: u32,
|
||||||
|
tp_mac: u16,
|
||||||
|
tp_net: u16,
|
||||||
|
tp_sec: u32,
|
||||||
|
tp_nsec: u32,
|
||||||
|
tp_vlan_tci: u16,
|
||||||
|
tp_vlan_tpid: u16,
|
||||||
|
tp_padding: [4]u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const TP_STATUS_AVAILABLE: u32 = 0;
|
||||||
|
const TP_STATUS_SEND_REQUEST: u32 = 1;
|
||||||
|
const DATA_OFFSET: usize = 32;
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(@sizeOf(tpacket2_hdr) == 32);
|
||||||
|
std.debug.assert(DATA_OFFSET == tpacketAlign(@sizeOf(tpacket2_hdr)));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Ring = struct {
|
||||||
|
buf: []u8,
|
||||||
|
frame_size: usize,
|
||||||
|
frame_nr: usize,
|
||||||
|
next: usize,
|
||||||
|
|
||||||
|
pub fn init(buf: []u8, frame_size: usize, frame_nr: usize) Ring {
|
||||||
|
return .{ .buf = buf, .frame_size = frame_size, .frame_nr = frame_nr, .next = 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn header(self: Ring, idx: usize) *tpacket2_hdr {
|
||||||
|
return @ptrCast(@alignCast(self.buf.ptr + idx * self.frame_size));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reserve(self: *Ring) ?usize {
|
||||||
|
const idx = self.next;
|
||||||
|
const hdr = self.header(idx);
|
||||||
|
if (@atomicLoad(u32, &hdr.tp_status, .monotonic) != TP_STATUS_AVAILABLE) return null;
|
||||||
|
self.next = (self.next + 1) % self.frame_nr;
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fill(self: *Ring, idx: usize, frame: []const u8) void {
|
||||||
|
std.debug.assert(frame.len + DATA_OFFSET <= self.frame_size);
|
||||||
|
const hdr = self.header(idx);
|
||||||
|
const data = self.buf[idx * self.frame_size + DATA_OFFSET ..][0..frame.len];
|
||||||
|
@memcpy(data, frame);
|
||||||
|
hdr.tp_len = @intCast(frame.len);
|
||||||
|
@atomicStore(u32, &hdr.tp_status, TP_STATUS_SEND_REQUEST, .monotonic);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
pub const OpenError = error{
|
||||||
|
NeedCapNetRaw,
|
||||||
|
SocketFailed,
|
||||||
|
IfIndexFailed,
|
||||||
|
SetSockOptFailed,
|
||||||
|
MmapFailed,
|
||||||
|
BindFailed,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const RingConfig = struct {
|
||||||
|
frame_size: u32 = 2048,
|
||||||
|
block_size: u32 = 1 << 22,
|
||||||
|
block_nr: u32 = 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Backend = struct {
|
||||||
|
fd: i32,
|
||||||
|
ring: Ring,
|
||||||
|
map: []align(std.heap.page_size_min) u8,
|
||||||
|
sll: linux.sockaddr.ll,
|
||||||
|
|
||||||
|
pub fn open(ifname: []const u8, cfg: RingConfig) OpenError!Backend {
|
||||||
|
const rc_sock = linux.socket(
|
||||||
|
linux.AF.PACKET,
|
||||||
|
linux.SOCK.RAW,
|
||||||
|
std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP)),
|
||||||
|
);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.SocketFailed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.IfIndexFailed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS)
|
||||||
|
return error.IfIndexFailed;
|
||||||
|
const ifindex: i32 = ifr.ifru.ivalue;
|
||||||
|
|
||||||
|
var ver: u32 = @intFromEnum(linux.tpacket_versions.V2);
|
||||||
|
if (linux.errno(linux.setsockopt(fd, linux.SOL.PACKET, linux.PACKET.VERSION, std.mem.asBytes(&ver), @sizeOf(u32))) != .SUCCESS)
|
||||||
|
return error.SetSockOptFailed;
|
||||||
|
|
||||||
|
const frame_nr = (cfg.block_size / cfg.frame_size) * cfg.block_nr;
|
||||||
|
var req = linux.tpacket_req3{
|
||||||
|
.block_size = cfg.block_size,
|
||||||
|
.block_nr = cfg.block_nr,
|
||||||
|
.frame_size = cfg.frame_size,
|
||||||
|
.frame_nr = frame_nr,
|
||||||
|
.retire_blk_tov = 0,
|
||||||
|
.sizeof_priv = 0,
|
||||||
|
.feature_req_word = 0,
|
||||||
|
};
|
||||||
|
if (linux.errno(linux.setsockopt(fd, linux.SOL.PACKET, linux.PACKET.TX_RING, std.mem.asBytes(&req), @sizeOf(@TypeOf(req)))) != .SUCCESS)
|
||||||
|
return error.SetSockOptFailed;
|
||||||
|
|
||||||
|
var bypass: u32 = 1;
|
||||||
|
_ = linux.setsockopt(fd, linux.SOL.PACKET, linux.PACKET.QDISC_BYPASS, std.mem.asBytes(&bypass), @sizeOf(u32));
|
||||||
|
|
||||||
|
const ring_sz: usize = @as(usize, cfg.block_size) * cfg.block_nr;
|
||||||
|
const rc_map = linux.mmap(null, ring_sz, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED }, fd, 0);
|
||||||
|
if (linux.errno(rc_map) != .SUCCESS) return error.MmapFailed;
|
||||||
|
const map_ptr: [*]align(std.heap.page_size_min) u8 = @ptrFromInt(rc_map);
|
||||||
|
const map = map_ptr[0..ring_sz];
|
||||||
|
errdefer _ = linux.munmap(map.ptr, ring_sz);
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP));
|
||||||
|
sll.ifindex = ifindex;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS)
|
||||||
|
return error.BindFailed;
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.fd = fd,
|
||||||
|
.ring = Ring.init(map, cfg.frame_size, frame_nr),
|
||||||
|
.map = map,
|
||||||
|
.sll = sll,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn submit(self: *Backend, frame: []const u8) bool {
|
||||||
|
const idx = self.ring.reserve() orelse return false;
|
||||||
|
self.ring.fill(idx, frame);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kick(self: *Backend) void {
|
||||||
|
var dummy: u8 = 0;
|
||||||
|
_ = linux.sendto(self.fd, @ptrCast(&dummy), 0, 0, @ptrCast(&self.sll), @sizeOf(linux.sockaddr.ll));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *Backend) void {
|
||||||
|
_ = linux.munmap(self.map.ptr, self.map.len);
|
||||||
|
_ = linux.close(self.fd);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "tpacket2_hdr is the wire-exact 32 bytes" {
|
||||||
|
try std.testing.expectEqual(@as(usize, 32), @sizeOf(tpacket2_hdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Ring reserve cycles all frames then returns null when full" {
|
||||||
|
const frame_size: usize = 2048;
|
||||||
|
const frame_nr: usize = 4;
|
||||||
|
const buf = try std.testing.allocator.alloc(u8, frame_size * frame_nr);
|
||||||
|
defer std.testing.allocator.free(buf);
|
||||||
|
@memset(buf, 0);
|
||||||
|
var ring = Ring.init(buf, frame_size, frame_nr);
|
||||||
|
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < frame_nr) : (i += 1) {
|
||||||
|
const idx = ring.reserve() orelse return error.UnexpectedFull;
|
||||||
|
ring.fill(idx, &[_]u8{ 0xAA, 0xBB });
|
||||||
|
}
|
||||||
|
try std.testing.expect(ring.reserve() == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "fill writes len, SEND_REQUEST status, and the frame bytes at the data offset" {
|
||||||
|
const frame_size: usize = 2048;
|
||||||
|
const buf = try std.testing.allocator.alloc(u8, frame_size);
|
||||||
|
defer std.testing.allocator.free(buf);
|
||||||
|
@memset(buf, 0);
|
||||||
|
var ring = Ring.init(buf, frame_size, 1);
|
||||||
|
|
||||||
|
const idx = ring.reserve().?;
|
||||||
|
const payload = [_]u8{ 1, 2, 3, 4, 5 };
|
||||||
|
ring.fill(idx, &payload);
|
||||||
|
|
||||||
|
const hdr: *const tpacket2_hdr = @ptrCast(@alignCast(buf.ptr));
|
||||||
|
try std.testing.expectEqual(@as(u32, payload.len), hdr.tp_len);
|
||||||
|
try std.testing.expectEqual(TP_STATUS_SEND_REQUEST, @atomicLoad(u32, &hdr.tp_status, .monotonic));
|
||||||
|
try std.testing.expectEqualSlices(u8, &payload, buf[DATA_OFFSET .. DATA_OFFSET + payload.len]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a kernel-drained slot (status reset to AVAILABLE) is reusable" {
|
||||||
|
const frame_size: usize = 2048;
|
||||||
|
const buf = try std.testing.allocator.alloc(u8, frame_size);
|
||||||
|
defer std.testing.allocator.free(buf);
|
||||||
|
@memset(buf, 0);
|
||||||
|
var ring = Ring.init(buf, frame_size, 1);
|
||||||
|
|
||||||
|
const idx0 = ring.reserve().?;
|
||||||
|
ring.fill(idx0, &[_]u8{0x01});
|
||||||
|
try std.testing.expect(ring.reserve() == null);
|
||||||
|
|
||||||
|
const hdr: *tpacket2_hdr = @ptrCast(@alignCast(buf.ptr));
|
||||||
|
@atomicStore(u32, &hdr.tp_status, TP_STATUS_AVAILABLE, .monotonic);
|
||||||
|
try std.testing.expect(ring.reserve() != null);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,220 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// afxdp.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const xdp = @import("xdp");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
const page = std.heap.page_size_min;
|
||||||
|
|
||||||
|
pub const Mode = enum { zerocopy, copy };
|
||||||
|
|
||||||
|
pub const OpenError = error{
|
||||||
|
NeedCapNetRaw,
|
||||||
|
SocketFailed,
|
||||||
|
IfIndexFailed,
|
||||||
|
UmemAllocFailed,
|
||||||
|
UmemRegFailed,
|
||||||
|
RingSetupFailed,
|
||||||
|
MmapOffsetsFailed,
|
||||||
|
RingMmapFailed,
|
||||||
|
BindFailed,
|
||||||
|
BadConfig,
|
||||||
|
OutOfMemory,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
frame_size: u32 = 2048,
|
||||||
|
num_frames: u32 = 4096,
|
||||||
|
tx_size: u32 = 2048,
|
||||||
|
comp_size: u32 = 2048,
|
||||||
|
fill_size: u32 = 2048,
|
||||||
|
queue_id: u32 = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn setU32(fd: i32, name: u32, val: u32) OpenError!void {
|
||||||
|
var v = val;
|
||||||
|
if (linux.errno(linux.setsockopt(fd, xdp.SOL_XDP, name, std.mem.asBytes(&v), @sizeOf(u32))) != .SUCCESS)
|
||||||
|
return error.RingSetupFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mapRing(fd: i32, len: usize, pg: u64) OpenError![]align(page) u8 {
|
||||||
|
const rc = linux.mmap(null, len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .POPULATE = true }, fd, @intCast(pg));
|
||||||
|
if (linux.errno(rc) != .SUCCESS) return error.RingMmapFailed;
|
||||||
|
const ptr: [*]align(page) u8 = @ptrFromInt(rc);
|
||||||
|
return ptr[0..len];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ringPtr(comptime T: type, map: []align(page) u8, offset: u64) T {
|
||||||
|
return @ptrCast(@alignCast(map.ptr + @as(usize, @intCast(offset))));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Backend = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
fd: i32,
|
||||||
|
mode: Mode,
|
||||||
|
frame_size: u32,
|
||||||
|
umem: []align(page) u8,
|
||||||
|
tx_map: []align(page) u8,
|
||||||
|
comp_map: []align(page) u8,
|
||||||
|
tx: xdp.Prod,
|
||||||
|
comp: xdp.Comp,
|
||||||
|
frames: xdp.FrameStack,
|
||||||
|
frame_backing: []u64,
|
||||||
|
|
||||||
|
pub fn open(allocator: std.mem.Allocator, ifname: []const u8, mode: Mode, cfg: Config) OpenError!Backend {
|
||||||
|
if (!std.math.isPowerOfTwo(cfg.tx_size) or !std.math.isPowerOfTwo(cfg.comp_size) or !std.math.isPowerOfTwo(cfg.fill_size))
|
||||||
|
return error.BadConfig;
|
||||||
|
if (cfg.frame_size != 2048 and cfg.frame_size != 4096) return error.BadConfig;
|
||||||
|
if (cfg.num_frames < cfg.tx_size or cfg.num_frames < cfg.comp_size) return error.BadConfig;
|
||||||
|
|
||||||
|
const rc_sock = linux.socket(xdp.AF_XDP, linux.SOCK.RAW, 0);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.SocketFailed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.IfIndexFailed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS)
|
||||||
|
return error.IfIndexFailed;
|
||||||
|
const ifindex: u32 = @intCast(ifr.ifru.ivalue);
|
||||||
|
|
||||||
|
const umem_len: usize = @as(usize, cfg.num_frames) * cfg.frame_size;
|
||||||
|
const rc_umem = linux.mmap(null, umem_len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, -1, 0);
|
||||||
|
if (linux.errno(rc_umem) != .SUCCESS) return error.UmemAllocFailed;
|
||||||
|
const umem_ptr: [*]align(page) u8 = @ptrFromInt(rc_umem);
|
||||||
|
const umem = umem_ptr[0..umem_len];
|
||||||
|
errdefer _ = linux.munmap(umem.ptr, umem.len);
|
||||||
|
|
||||||
|
var reg = xdp.UmemReg{
|
||||||
|
.addr = @intFromPtr(umem.ptr),
|
||||||
|
.len = umem_len,
|
||||||
|
.chunk_size = cfg.frame_size,
|
||||||
|
.headroom = 0,
|
||||||
|
.flags = 0,
|
||||||
|
.tx_metadata_len = 0,
|
||||||
|
};
|
||||||
|
if (linux.errno(linux.setsockopt(fd, xdp.SOL_XDP, xdp.sockopt.UMEM_REG, std.mem.asBytes(®), @sizeOf(xdp.UmemReg))) != .SUCCESS)
|
||||||
|
return error.UmemRegFailed;
|
||||||
|
|
||||||
|
try setU32(fd, xdp.sockopt.UMEM_FILL_RING, cfg.fill_size);
|
||||||
|
try setU32(fd, xdp.sockopt.UMEM_COMPLETION_RING, cfg.comp_size);
|
||||||
|
try setU32(fd, xdp.sockopt.TX_RING, cfg.tx_size);
|
||||||
|
|
||||||
|
var off = std.mem.zeroes(xdp.MmapOffsets);
|
||||||
|
var off_len: linux.socklen_t = @sizeOf(xdp.MmapOffsets);
|
||||||
|
if (linux.errno(linux.getsockopt(fd, xdp.SOL_XDP, xdp.sockopt.MMAP_OFFSETS, std.mem.asBytes(&off), &off_len)) != .SUCCESS)
|
||||||
|
return error.MmapOffsetsFailed;
|
||||||
|
|
||||||
|
const comp_len: usize = @as(usize, @intCast(off.cr.desc)) + @as(usize, cfg.comp_size) * @sizeOf(u64);
|
||||||
|
const comp_map = try mapRing(fd, comp_len, xdp.pgoff.COMPLETION_RING);
|
||||||
|
errdefer _ = linux.munmap(comp_map.ptr, comp_map.len);
|
||||||
|
|
||||||
|
const tx_len: usize = @as(usize, @intCast(off.tx.desc)) + @as(usize, cfg.tx_size) * @sizeOf(xdp.Desc);
|
||||||
|
const tx_map = try mapRing(fd, tx_len, xdp.pgoff.TX_RING);
|
||||||
|
errdefer _ = linux.munmap(tx_map.ptr, tx_map.len);
|
||||||
|
|
||||||
|
const frame_backing = try allocator.alloc(u64, cfg.num_frames);
|
||||||
|
errdefer allocator.free(frame_backing);
|
||||||
|
|
||||||
|
const tx = xdp.Prod{
|
||||||
|
.producer = ringPtr(*u32, tx_map, off.tx.producer),
|
||||||
|
.consumer = ringPtr(*u32, tx_map, off.tx.consumer),
|
||||||
|
.ring = ringPtr([*]xdp.Desc, tx_map, off.tx.desc),
|
||||||
|
.mask = cfg.tx_size - 1,
|
||||||
|
.size = cfg.tx_size,
|
||||||
|
};
|
||||||
|
const comp = xdp.Comp{
|
||||||
|
.producer = ringPtr(*u32, comp_map, off.cr.producer),
|
||||||
|
.consumer = ringPtr(*u32, comp_map, off.cr.consumer),
|
||||||
|
.ring = ringPtr([*]u64, comp_map, off.cr.desc),
|
||||||
|
.mask = cfg.comp_size - 1,
|
||||||
|
.size = cfg.comp_size,
|
||||||
|
};
|
||||||
|
|
||||||
|
const zc: u16 = if (mode == .zerocopy) xdp.bind_flags.ZEROCOPY else xdp.bind_flags.COPY;
|
||||||
|
var sxdp = linux.sockaddr.xdp{
|
||||||
|
.flags = xdp.bind_flags.USE_NEED_WAKEUP | zc,
|
||||||
|
.ifindex = ifindex,
|
||||||
|
.queue_id = cfg.queue_id,
|
||||||
|
.shared_umem_fd = 0,
|
||||||
|
};
|
||||||
|
switch (linux.errno(linux.bind(fd, @ptrCast(&sxdp), @sizeOf(linux.sockaddr.xdp)))) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.BindFailed,
|
||||||
|
}
|
||||||
|
|
||||||
|
var actual_mode = mode;
|
||||||
|
var xopt = xdp.Options{ .flags = 0 };
|
||||||
|
var xopt_len: linux.socklen_t = @sizeOf(xdp.Options);
|
||||||
|
if (linux.errno(linux.getsockopt(fd, xdp.SOL_XDP, xdp.sockopt.OPTIONS, std.mem.asBytes(&xopt), &xopt_len)) == .SUCCESS) {
|
||||||
|
actual_mode = if ((xopt.flags & xdp.OPTIONS_ZEROCOPY) != 0) .zerocopy else .copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
|
.fd = fd,
|
||||||
|
.mode = actual_mode,
|
||||||
|
.frame_size = cfg.frame_size,
|
||||||
|
.umem = umem,
|
||||||
|
.tx_map = tx_map,
|
||||||
|
.comp_map = comp_map,
|
||||||
|
.tx = tx,
|
||||||
|
.comp = comp,
|
||||||
|
.frames = xdp.FrameStack.init(frame_backing, cfg.num_frames, cfg.frame_size),
|
||||||
|
.frame_backing = frame_backing,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn submit(self: *Backend, frame: []const u8) bool {
|
||||||
|
if (frame.len > self.frame_size) return false;
|
||||||
|
const off = self.frames.pop() orelse return false;
|
||||||
|
const slot = self.tx.reserve() orelse {
|
||||||
|
self.frames.push(off);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
const o: usize = @intCast(off);
|
||||||
|
@memcpy(self.umem[o..][0..frame.len], frame);
|
||||||
|
self.tx.write(slot, .{ .addr = off, .len = @intCast(frame.len), .options = 0 });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kick(self: *Backend) void {
|
||||||
|
self.tx.publish();
|
||||||
|
var dummy: u8 = 0;
|
||||||
|
_ = linux.sendto(self.fd, @ptrCast(&dummy), 0, linux.MSG.DONTWAIT, null, 0);
|
||||||
|
const n = self.comp.peek();
|
||||||
|
var i: u32 = 0;
|
||||||
|
while (i < n) : (i += 1) {
|
||||||
|
self.frames.push(self.comp.addrAt(i));
|
||||||
|
}
|
||||||
|
if (n > 0) self.comp.release(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *Backend) void {
|
||||||
|
_ = linux.munmap(self.tx_map.ptr, self.tx_map.len);
|
||||||
|
_ = linux.munmap(self.comp_map.ptr, self.comp_map.len);
|
||||||
|
_ = linux.munmap(self.umem.ptr, self.umem.len);
|
||||||
|
self.allocator.free(self.frame_backing);
|
||||||
|
_ = linux.close(self.fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn zerocopy(self: *const Backend) bool {
|
||||||
|
return self.mode == .zerocopy;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "Config rejects non-power-of-two rings and illegal chunk sizes" {
|
||||||
|
const bad_ring = Backend.open(std.testing.allocator, "lo", .copy, .{ .tx_size = 1000 });
|
||||||
|
try std.testing.expectError(error.BadConfig, bad_ring);
|
||||||
|
const bad_chunk = Backend.open(std.testing.allocator, "lo", .copy, .{ .frame_size = 1024 });
|
||||||
|
try std.testing.expectError(error.BadConfig, bad_chunk);
|
||||||
|
const too_few = Backend.open(std.testing.allocator, "lo", .copy, .{ .num_frames = 512, .tx_size = 2048 });
|
||||||
|
try std.testing.expectError(error.BadConfig, too_few);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,194 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// bench.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const packet = @import("packet");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const targets = @import("targets");
|
||||||
|
const template = @import("template");
|
||||||
|
const dedup = @import("dedup");
|
||||||
|
|
||||||
|
const warmup_frac: u64 = 16;
|
||||||
|
const iters_checksum_small: u64 = 100_000_000;
|
||||||
|
const iters_checksum_mtu: u64 = 5_000_000;
|
||||||
|
const iters_cookie: u64 = 100_000_000;
|
||||||
|
const iters_stamp: u64 = 30_000_000;
|
||||||
|
const dedup_keys: u64 = 4_000_000;
|
||||||
|
const dedup_cap: usize = 1 << 23;
|
||||||
|
const spread_multiplier: u64 = 0x9E3779B97F4A7C15;
|
||||||
|
const engine_cidr = "1.0.0.0/8";
|
||||||
|
const engine_port: u16 = 80;
|
||||||
|
const engine_seed: u64 = 0xC0FFEE;
|
||||||
|
const small_len: usize = 40;
|
||||||
|
const mtu_len: usize = 1500;
|
||||||
|
const cookie_key = [_]u8{0x42} ** 16;
|
||||||
|
const bench_src_ip: u32 = 0x0A000001;
|
||||||
|
const bench_src_port: u16 = 0xC000;
|
||||||
|
const bench_them_ip: u32 = 0x08080808;
|
||||||
|
const bench_dst_ip: u32 = 0x01020304;
|
||||||
|
const bench_src_mac = [_]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 };
|
||||||
|
const bench_dst_mac = [_]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x02 };
|
||||||
|
|
||||||
|
fn monoNow() u64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn report(name: []const u8, iters: u64, ns_total: u64, bytes_per_op: u64) void {
|
||||||
|
if (iters == 0 or ns_total == 0) {
|
||||||
|
std.debug.print(" {s:<38} no measurement (0 iterations or 0 ns)\n", .{name});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const fi: f64 = @floatFromInt(iters);
|
||||||
|
const fns: f64 = @floatFromInt(ns_total);
|
||||||
|
const ns_per = fns / fi;
|
||||||
|
const mops = fi / (fns / 1e9) / 1e6;
|
||||||
|
if (bytes_per_op > 0) {
|
||||||
|
const total_bytes: f64 = @floatFromInt(iters * bytes_per_op);
|
||||||
|
const gbps = total_bytes / fns;
|
||||||
|
std.debug.print(" {s:<38} {d:>9.2} ns/op {d:>9.1} Mops/s {d:>7.2} GB/s\n", .{ name, ns_per, mops, gbps });
|
||||||
|
} else {
|
||||||
|
std.debug.print(" {s:<38} {d:>9.2} ns/op {d:>9.1} Mops/s\n", .{ name, ns_per, mops });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchChecksum(name: []const u8, iters: u64, buf: []u8, comptime f: fn ([]const u8) u16) u64 {
|
||||||
|
var acc: u64 = 0;
|
||||||
|
var w: u64 = 0;
|
||||||
|
while (w < iters / warmup_frac) : (w += 1) {
|
||||||
|
buf[0] = @truncate(w);
|
||||||
|
acc +%= f(buf);
|
||||||
|
}
|
||||||
|
acc = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
var i: u64 = 0;
|
||||||
|
while (i < iters) : (i += 1) {
|
||||||
|
buf[0] = @truncate(i);
|
||||||
|
buf[buf.len - 1] = @truncate(i >> 8);
|
||||||
|
acc +%= f(buf);
|
||||||
|
}
|
||||||
|
report(name, iters, monoNow() - t0, buf.len);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchCookieGen(ck: cookie.Cookie) u64 {
|
||||||
|
var acc: u64 = 0;
|
||||||
|
var w: u64 = 0;
|
||||||
|
while (w < iters_cookie / warmup_frac) : (w += 1) {
|
||||||
|
acc +%= ck.generate(bench_them_ip +% @as(u32, @truncate(w)), @truncate(w), bench_src_ip, bench_src_port);
|
||||||
|
}
|
||||||
|
acc = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
var i: u64 = 0;
|
||||||
|
while (i < iters_cookie) : (i += 1) {
|
||||||
|
const ip: u32 = bench_them_ip +% @as(u32, @truncate(i));
|
||||||
|
acc +%= ck.generate(ip, @truncate(i), bench_src_ip, bench_src_port);
|
||||||
|
}
|
||||||
|
report("cookie generate (SipHash)", iters_cookie, monoNow() - t0, 0);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchCookieValidate(ck: cookie.Cookie) u64 {
|
||||||
|
var acc: u64 = 0;
|
||||||
|
var w: u64 = 0;
|
||||||
|
while (w < iters_cookie / warmup_frac) : (w += 1) {
|
||||||
|
const ip: u32 = bench_them_ip +% @as(u32, @truncate(w));
|
||||||
|
const ack: u32 = @truncate(w *% spread_multiplier);
|
||||||
|
acc +%= @intFromBool(ck.validateSynAck(ack, ip, @truncate(w), bench_src_ip, bench_src_port));
|
||||||
|
}
|
||||||
|
acc = 0;
|
||||||
|
var i: u64 = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
while (i < iters_cookie) : (i += 1) {
|
||||||
|
const ip: u32 = bench_them_ip +% @as(u32, @truncate(i));
|
||||||
|
const ack: u32 = @truncate(i *% spread_multiplier);
|
||||||
|
acc +%= @intFromBool(ck.validateSynAck(ack, ip, @truncate(i), bench_src_ip, bench_src_port));
|
||||||
|
}
|
||||||
|
report("cookie validate (SipHash)", iters_cookie, monoNow() - t0, 0);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchStamp() u64 {
|
||||||
|
const ck = cookie.Cookie.init(cookie_key);
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = bench_src_mac,
|
||||||
|
.dst_mac = bench_dst_mac,
|
||||||
|
.src_ip = bench_src_ip,
|
||||||
|
.src_port = bench_src_port,
|
||||||
|
.cookie = ck,
|
||||||
|
});
|
||||||
|
var out: [template.SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var acc: u64 = 0;
|
||||||
|
var w: u64 = 0;
|
||||||
|
while (w < iters_stamp / warmup_frac) : (w += 1) {
|
||||||
|
const n = tmpl.stamp(&out, bench_dst_ip +% @as(u32, @truncate(w)), @truncate(w));
|
||||||
|
acc +%= out[n - 1];
|
||||||
|
}
|
||||||
|
acc = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
var i: u64 = 0;
|
||||||
|
while (i < iters_stamp) : (i += 1) {
|
||||||
|
const ip: u32 = bench_dst_ip +% @as(u32, @truncate(i));
|
||||||
|
const n = tmpl.stamp(&out, ip, @truncate(i));
|
||||||
|
acc +%= @as(u64, out[n - 1]) +% n;
|
||||||
|
}
|
||||||
|
report("SYN template stamp (full frame)", iters_stamp, monoNow() - t0, 0);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchEngine(alloc: std.mem.Allocator) !u64 {
|
||||||
|
const range = try targets.parseCidr(engine_cidr);
|
||||||
|
const ports = [_]u16{engine_port};
|
||||||
|
var eng = try targets.Engine.init(alloc, &.{range}, &ports, engine_seed);
|
||||||
|
defer eng.deinit();
|
||||||
|
var acc: u64 = 0;
|
||||||
|
var count: u64 = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
while (eng.next()) |t| {
|
||||||
|
acc +%= @as(u64, t.ip) +% t.port;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
report("address engine next() [/8 traversal]", count, monoNow() - t0, 0);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn benchDedup(alloc: std.mem.Allocator) !u64 {
|
||||||
|
var d = try dedup.Dedup.init(alloc, dedup_cap);
|
||||||
|
defer d.deinit();
|
||||||
|
var acc: u64 = 0;
|
||||||
|
const t0 = monoNow();
|
||||||
|
var i: u64 = 0;
|
||||||
|
while (i < dedup_keys) : (i += 1) {
|
||||||
|
acc +%= @intFromBool(d.insert(i *% spread_multiplier));
|
||||||
|
}
|
||||||
|
report("dedup insert (empty -> 4M, 2^23 slots)", dedup_keys, monoNow() - t0, 0);
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
const alloc = std.heap.page_allocator;
|
||||||
|
var sink: u64 = 0;
|
||||||
|
|
||||||
|
std.debug.print("\nzingela hot-path microbenchmarks (ReleaseFast, this host)\n", .{});
|
||||||
|
std.debug.print("each op varies its input per iteration; results fold into a printed sink to defeat elision\n\n", .{});
|
||||||
|
|
||||||
|
var buf: [mtu_len]u8 = undefined;
|
||||||
|
for (&buf, 0..) |*byte, i| byte.* = @truncate(i *% 131 +% 7);
|
||||||
|
|
||||||
|
sink +%= benchChecksum("RFC1071 checksum scalar (40B)", iters_checksum_small, buf[0..small_len], packet.checksum);
|
||||||
|
sink +%= benchChecksum("RFC1071 checksum SIMD (40B)", iters_checksum_small, buf[0..small_len], packet.checksumSimd);
|
||||||
|
sink +%= benchChecksum("RFC1071 checksum scalar (1500B)", iters_checksum_mtu, buf[0..mtu_len], packet.checksum);
|
||||||
|
sink +%= benchChecksum("RFC1071 checksum SIMD (1500B)", iters_checksum_mtu, buf[0..mtu_len], packet.checksumSimd);
|
||||||
|
|
||||||
|
const ck = cookie.Cookie.init(cookie_key);
|
||||||
|
sink +%= benchCookieGen(ck);
|
||||||
|
sink +%= benchCookieValidate(ck);
|
||||||
|
|
||||||
|
sink +%= benchStamp();
|
||||||
|
sink +%= try benchEngine(alloc);
|
||||||
|
sink +%= try benchDedup(alloc);
|
||||||
|
|
||||||
|
std.debug.print("\nanti-elision sink: 0x{x}\n", .{sink});
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,785 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// classify.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const packet = @import("packet");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
|
||||||
|
pub const State = enum { open, closed, filtered, unfiltered };
|
||||||
|
|
||||||
|
pub const Result = struct {
|
||||||
|
addr: packet.Addr,
|
||||||
|
port: u16,
|
||||||
|
state: State,
|
||||||
|
|
||||||
|
pub fn v4(ip: u32, port: u16, state: State) Result {
|
||||||
|
return .{ .addr = .{ .v4 = ip }, .port = port, .state = state };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn v6(a: [16]u8, port: u16, state: State) Result {
|
||||||
|
return .{ .addr = .{ .v6 = a }, .port = port, .state = state };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const ETH_HDR_LEN: usize = 14;
|
||||||
|
const ETH_OFF_TYPE: usize = 12;
|
||||||
|
const ETHERTYPE_IPV4: u16 = 0x0800;
|
||||||
|
|
||||||
|
const IPPROTO_TCP: u8 = 6;
|
||||||
|
const IPPROTO_UDP: u8 = 17;
|
||||||
|
const IPPROTO_ICMP: u8 = 1;
|
||||||
|
const IP_MIN_IHL: usize = 20;
|
||||||
|
const IP_IHL_MASK: u8 = 0x0f;
|
||||||
|
const IP_WORD_BYTES: usize = 4;
|
||||||
|
const IP_OFF_PROTO: usize = 9;
|
||||||
|
const IP_OFF_SRC: usize = 12;
|
||||||
|
const IP_OFF_DST: usize = 16;
|
||||||
|
|
||||||
|
const INNER_TCP_MIN_LEN: usize = 8;
|
||||||
|
const INNER_UDP_MIN_LEN: usize = 8;
|
||||||
|
|
||||||
|
const UDP_OFF_SPORT: usize = 0;
|
||||||
|
const UDP_OFF_DPORT: usize = 2;
|
||||||
|
const UDP_MIN_LEN: usize = 8;
|
||||||
|
|
||||||
|
const ICMP_CODE_PORT_UNREACH: u8 = 3;
|
||||||
|
|
||||||
|
const TCP_OFF_SPORT: usize = 0;
|
||||||
|
const TCP_OFF_DPORT: usize = 2;
|
||||||
|
const TCP_OFF_SEQ: usize = 4;
|
||||||
|
const TCP_OFF_ACK: usize = 8;
|
||||||
|
const TCP_OFF_FLAGS: usize = 13;
|
||||||
|
const TCP_OFF_WINDOW: usize = 14;
|
||||||
|
const TCP_MIN_LEN: usize = 20;
|
||||||
|
|
||||||
|
const TCP_FLAG_SYN: u8 = 0x02;
|
||||||
|
const TCP_FLAG_RST: u8 = 0x04;
|
||||||
|
const TCP_FLAG_ACK: u8 = 0x10;
|
||||||
|
|
||||||
|
const ICMP_HDR_LEN: usize = 8;
|
||||||
|
const ICMP_TYPE_DEST_UNREACH: u8 = 3;
|
||||||
|
const ICMP_OFF_TYPE: usize = 0;
|
||||||
|
const ICMP_OFF_CODE: usize = 1;
|
||||||
|
|
||||||
|
const ETHERTYPE_IPV6: u16 = 0x86dd;
|
||||||
|
const IP6_HDR_LEN: usize = 40;
|
||||||
|
const IP6_OFF_NEXT: usize = 6;
|
||||||
|
const IP6_OFF_SRC: usize = 8;
|
||||||
|
const IP6_OFF_DST: usize = 24;
|
||||||
|
const IPPROTO_ICMPV6: u8 = 58;
|
||||||
|
const ICMP6_TYPE_DEST_UNREACH: u8 = 1;
|
||||||
|
const ICMP6_ERR_HDR_LEN: usize = 8;
|
||||||
|
|
||||||
|
fn icmpCodeIsFilteredForSyn(code: u8) bool {
|
||||||
|
return switch (code) {
|
||||||
|
1, 2, 3, 9, 10, 13 => true,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn icmpCodeIsFilteredForUdp(code: u8) bool {
|
||||||
|
return switch (code) {
|
||||||
|
1, 2, 9, 10, 13 => true,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ihlBytes(first_byte: u8) usize {
|
||||||
|
return @as(usize, first_byte & IP_IHL_MASK) * IP_WORD_BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classify(frame: []const u8, ck: cookie.Cookie) ?Result {
|
||||||
|
return classifyTcp(frame, ck, .syn);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classifyTcp(frame: []const u8, ck: cookie.Cookie, scan: packet.ScanType) ?Result {
|
||||||
|
if (frame.len < ETH_HDR_LEN + IP_MIN_IHL) return null;
|
||||||
|
if (std.mem.readInt(u16, frame[ETH_OFF_TYPE..][0..2], .big) != ETHERTYPE_IPV4) return null;
|
||||||
|
|
||||||
|
const ip = ETH_HDR_LEN;
|
||||||
|
const ihl = ihlBytes(frame[ip]);
|
||||||
|
if (ihl < IP_MIN_IHL or frame.len < ip + ihl) return null;
|
||||||
|
|
||||||
|
const proto = frame[ip + IP_OFF_PROTO];
|
||||||
|
const ip_src = std.mem.readInt(u32, frame[ip + IP_OFF_SRC ..][0..4], .big);
|
||||||
|
const ip_dst = std.mem.readInt(u32, frame[ip + IP_OFF_DST ..][0..4], .big);
|
||||||
|
|
||||||
|
if (proto == IPPROTO_TCP) {
|
||||||
|
const tcp = ip + ihl;
|
||||||
|
if (frame.len < tcp + TCP_MIN_LEN) return null;
|
||||||
|
const sport = std.mem.readInt(u16, frame[tcp + TCP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const dport = std.mem.readInt(u16, frame[tcp + TCP_OFF_DPORT ..][0..2], .big);
|
||||||
|
const seqno = std.mem.readInt(u32, frame[tcp + TCP_OFF_SEQ ..][0..4], .big);
|
||||||
|
const ackno = std.mem.readInt(u32, frame[tcp + TCP_OFF_ACK ..][0..4], .big);
|
||||||
|
const flags = frame[tcp + TCP_OFF_FLAGS];
|
||||||
|
const window = std.mem.readInt(u16, frame[tcp + TCP_OFF_WINDOW ..][0..2], .big);
|
||||||
|
|
||||||
|
const cookie_val = ck.seq(ip_src, sport, ip_dst, dport);
|
||||||
|
const has_rst = (flags & TCP_FLAG_RST) != 0;
|
||||||
|
|
||||||
|
switch (scan) {
|
||||||
|
.syn => {
|
||||||
|
const is_synack = (flags & (TCP_FLAG_SYN | TCP_FLAG_ACK)) == (TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
if (is_synack and ackno == cookie_val +% 1)
|
||||||
|
return Result.v4(ip_src, sport, .open);
|
||||||
|
if (has_rst and (flags & TCP_FLAG_ACK) != 0 and ackno == cookie_val +% 1)
|
||||||
|
return Result.v4(ip_src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.fin, .null_scan, .xmas => {
|
||||||
|
if (has_rst and ackno == cookie_val +% scan.seqConsumed())
|
||||||
|
return Result.v4(ip_src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.maimon => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v4(ip_src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.ack => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v4(ip_src, sport, .unfiltered);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.window => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v4(ip_src, sport, if (window != 0) .open else .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (proto == IPPROTO_ICMP) {
|
||||||
|
const icmp = ip + ihl;
|
||||||
|
if (frame.len < icmp + ICMP_HDR_LEN) return null;
|
||||||
|
if (frame[icmp + ICMP_OFF_TYPE] != ICMP_TYPE_DEST_UNREACH) return null;
|
||||||
|
if (!icmpCodeIsFilteredForSyn(frame[icmp + ICMP_OFF_CODE])) return null;
|
||||||
|
|
||||||
|
const inner = icmp + ICMP_HDR_LEN;
|
||||||
|
if (frame.len < inner + IP_MIN_IHL) return null;
|
||||||
|
const inner_ihl = ihlBytes(frame[inner]);
|
||||||
|
if (inner_ihl < IP_MIN_IHL) return null;
|
||||||
|
if (frame[inner + IP_OFF_PROTO] != IPPROTO_TCP) return null;
|
||||||
|
|
||||||
|
const inner_src = std.mem.readInt(u32, frame[inner + IP_OFF_SRC ..][0..4], .big);
|
||||||
|
const inner_dst = std.mem.readInt(u32, frame[inner + IP_OFF_DST ..][0..4], .big);
|
||||||
|
const inner_tcp = inner + inner_ihl;
|
||||||
|
if (frame.len < inner_tcp + INNER_TCP_MIN_LEN) return null;
|
||||||
|
const inner_sport = std.mem.readInt(u16, frame[inner_tcp + TCP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const inner_dport = std.mem.readInt(u16, frame[inner_tcp + TCP_OFF_DPORT ..][0..2], .big);
|
||||||
|
const inner_seq = std.mem.readInt(u32, frame[inner_tcp + TCP_OFF_SEQ ..][0..4], .big);
|
||||||
|
|
||||||
|
if (inner_seq == ck.seq(inner_dst, inner_dport, inner_src, inner_sport))
|
||||||
|
return Result.v4(inner_dst, inner_dport, .filtered);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classifyUdp(frame: []const u8, ck: cookie.Cookie, base: u16, span: u16) ?Result {
|
||||||
|
if (frame.len < ETH_HDR_LEN + IP_MIN_IHL) return null;
|
||||||
|
if (std.mem.readInt(u16, frame[ETH_OFF_TYPE..][0..2], .big) != ETHERTYPE_IPV4) return null;
|
||||||
|
|
||||||
|
const ip = ETH_HDR_LEN;
|
||||||
|
const ihl = ihlBytes(frame[ip]);
|
||||||
|
if (ihl < IP_MIN_IHL or frame.len < ip + ihl) return null;
|
||||||
|
|
||||||
|
const proto = frame[ip + IP_OFF_PROTO];
|
||||||
|
const ip_src = std.mem.readInt(u32, frame[ip + IP_OFF_SRC ..][0..4], .big);
|
||||||
|
const ip_dst = std.mem.readInt(u32, frame[ip + IP_OFF_DST ..][0..4], .big);
|
||||||
|
|
||||||
|
if (proto == IPPROTO_UDP) {
|
||||||
|
const udp = ip + ihl;
|
||||||
|
if (frame.len < udp + UDP_MIN_LEN) return null;
|
||||||
|
const sport = std.mem.readInt(u16, frame[udp + UDP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const dport = std.mem.readInt(u16, frame[udp + UDP_OFF_DPORT ..][0..2], .big);
|
||||||
|
if (dport == ck.udpSrcPort(ip_src, sport, ip_dst, base, span))
|
||||||
|
return Result.v4(ip_src, sport, .open);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (proto == IPPROTO_ICMP) {
|
||||||
|
const icmp = ip + ihl;
|
||||||
|
if (frame.len < icmp + ICMP_HDR_LEN) return null;
|
||||||
|
if (frame[icmp + ICMP_OFF_TYPE] != ICMP_TYPE_DEST_UNREACH) return null;
|
||||||
|
const code = frame[icmp + ICMP_OFF_CODE];
|
||||||
|
const state: State = if (code == ICMP_CODE_PORT_UNREACH)
|
||||||
|
.closed
|
||||||
|
else if (icmpCodeIsFilteredForUdp(code))
|
||||||
|
.filtered
|
||||||
|
else
|
||||||
|
return null;
|
||||||
|
|
||||||
|
const inner = icmp + ICMP_HDR_LEN;
|
||||||
|
if (frame.len < inner + IP_MIN_IHL) return null;
|
||||||
|
const inner_ihl = ihlBytes(frame[inner]);
|
||||||
|
if (inner_ihl < IP_MIN_IHL) return null;
|
||||||
|
if (frame[inner + IP_OFF_PROTO] != IPPROTO_UDP) return null;
|
||||||
|
|
||||||
|
const inner_src = std.mem.readInt(u32, frame[inner + IP_OFF_SRC ..][0..4], .big);
|
||||||
|
const inner_dst = std.mem.readInt(u32, frame[inner + IP_OFF_DST ..][0..4], .big);
|
||||||
|
const inner_udp = inner + inner_ihl;
|
||||||
|
if (frame.len < inner_udp + INNER_UDP_MIN_LEN) return null;
|
||||||
|
const inner_sport = std.mem.readInt(u16, frame[inner_udp + UDP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const inner_dport = std.mem.readInt(u16, frame[inner_udp + UDP_OFF_DPORT ..][0..2], .big);
|
||||||
|
|
||||||
|
if (inner_sport == ck.udpSrcPort(inner_dst, inner_dport, inner_src, base, span))
|
||||||
|
return Result.v4(inner_dst, inner_dport, state);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classifyTcp6(frame: []const u8, ck: cookie.Cookie, scan: packet.ScanType) ?Result {
|
||||||
|
if (frame.len < ETH_HDR_LEN + IP6_HDR_LEN) return null;
|
||||||
|
if (std.mem.readInt(u16, frame[ETH_OFF_TYPE..][0..2], .big) != ETHERTYPE_IPV6) return null;
|
||||||
|
|
||||||
|
const ip = ETH_HDR_LEN;
|
||||||
|
const next = frame[ip + IP6_OFF_NEXT];
|
||||||
|
const src: [16]u8 = frame[ip + IP6_OFF_SRC ..][0..16].*;
|
||||||
|
const dst: [16]u8 = frame[ip + IP6_OFF_DST ..][0..16].*;
|
||||||
|
|
||||||
|
if (next == IPPROTO_TCP) {
|
||||||
|
const tcp = ip + IP6_HDR_LEN;
|
||||||
|
if (frame.len < tcp + TCP_MIN_LEN) return null;
|
||||||
|
const sport = std.mem.readInt(u16, frame[tcp + TCP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const dport = std.mem.readInt(u16, frame[tcp + TCP_OFF_DPORT ..][0..2], .big);
|
||||||
|
const seqno = std.mem.readInt(u32, frame[tcp + TCP_OFF_SEQ ..][0..4], .big);
|
||||||
|
const ackno = std.mem.readInt(u32, frame[tcp + TCP_OFF_ACK ..][0..4], .big);
|
||||||
|
const flags = frame[tcp + TCP_OFF_FLAGS];
|
||||||
|
const window = std.mem.readInt(u16, frame[tcp + TCP_OFF_WINDOW ..][0..2], .big);
|
||||||
|
|
||||||
|
const cookie_val = ck.seq6(src, sport, dst, dport);
|
||||||
|
const has_rst = (flags & TCP_FLAG_RST) != 0;
|
||||||
|
|
||||||
|
switch (scan) {
|
||||||
|
.syn => {
|
||||||
|
const is_synack = (flags & (TCP_FLAG_SYN | TCP_FLAG_ACK)) == (TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
if (is_synack and ackno == cookie_val +% 1)
|
||||||
|
return Result.v6(src, sport, .open);
|
||||||
|
if (has_rst and (flags & TCP_FLAG_ACK) != 0 and ackno == cookie_val +% 1)
|
||||||
|
return Result.v6(src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.fin, .null_scan, .xmas => {
|
||||||
|
if (has_rst and ackno == cookie_val +% scan.seqConsumed())
|
||||||
|
return Result.v6(src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.maimon => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v6(src, sport, .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.ack => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v6(src, sport, .unfiltered);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
.window => {
|
||||||
|
if (has_rst and seqno == cookie_val)
|
||||||
|
return Result.v6(src, sport, if (window != 0) .open else .closed);
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next == IPPROTO_ICMPV6) {
|
||||||
|
const icmp = ip + IP6_HDR_LEN;
|
||||||
|
if (frame.len < icmp + ICMP6_ERR_HDR_LEN) return null;
|
||||||
|
if (frame[icmp + ICMP_OFF_TYPE] != ICMP6_TYPE_DEST_UNREACH) return null;
|
||||||
|
|
||||||
|
const inner = icmp + ICMP6_ERR_HDR_LEN;
|
||||||
|
if (frame.len < inner + IP6_HDR_LEN) return null;
|
||||||
|
if (frame[inner + IP6_OFF_NEXT] != IPPROTO_TCP) return null;
|
||||||
|
|
||||||
|
const inner_src: [16]u8 = frame[inner + IP6_OFF_SRC ..][0..16].*;
|
||||||
|
const inner_dst: [16]u8 = frame[inner + IP6_OFF_DST ..][0..16].*;
|
||||||
|
const inner_tcp = inner + IP6_HDR_LEN;
|
||||||
|
if (frame.len < inner_tcp + INNER_TCP_MIN_LEN) return null;
|
||||||
|
const inner_sport = std.mem.readInt(u16, frame[inner_tcp + TCP_OFF_SPORT ..][0..2], .big);
|
||||||
|
const inner_dport = std.mem.readInt(u16, frame[inner_tcp + TCP_OFF_DPORT ..][0..2], .big);
|
||||||
|
const inner_seq = std.mem.readInt(u32, frame[inner_tcp + TCP_OFF_SEQ ..][0..4], .big);
|
||||||
|
|
||||||
|
if (inner_seq == ck.seq6(inner_dst, inner_dport, inner_src, inner_sport))
|
||||||
|
return Result.v6(inner_dst, inner_dport, .filtered);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TcpClassifier = struct {
|
||||||
|
ck: cookie.Cookie,
|
||||||
|
scan: packet.ScanType = .syn,
|
||||||
|
|
||||||
|
pub fn match(self: TcpClassifier, frame: []const u8) ?Result {
|
||||||
|
return classifyTcp(frame, self.ck, self.scan);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const TcpClassifier6 = struct {
|
||||||
|
ck: cookie.Cookie,
|
||||||
|
scan: packet.ScanType = .syn,
|
||||||
|
|
||||||
|
pub fn match(self: TcpClassifier6, frame: []const u8) ?Result {
|
||||||
|
return classifyTcp6(frame, self.ck, self.scan);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const UdpClassifier = struct {
|
||||||
|
ck: cookie.Cookie,
|
||||||
|
base: u16,
|
||||||
|
span: u16,
|
||||||
|
|
||||||
|
pub fn match(self: UdpClassifier, frame: []const u8) ?Result {
|
||||||
|
return classifyUdp(frame, self.ck, self.base, self.span);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(@sizeOf(packet.EthHdr) == ETH_HDR_LEN);
|
||||||
|
std.debug.assert(@sizeOf(packet.Ipv4Hdr) == IP_MIN_IHL);
|
||||||
|
std.debug.assert(@sizeOf(packet.TcpHdr) == TCP_MIN_LEN);
|
||||||
|
std.debug.assert(@sizeOf(packet.UdpHdr) == UDP_MIN_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const our_ip: u32 = 0x0a000001;
|
||||||
|
const our_port: u16 = 40000;
|
||||||
|
const their_ip: u32 = 0x08080808;
|
||||||
|
const their_port: u16 = 80;
|
||||||
|
|
||||||
|
fn buildTcpReply(buf: *[54]u8, ip_src: u32, ip_dst: u32, sport: u16, dport: u16, seq: u32, ack: u32, flags: u8) void {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x45;
|
||||||
|
buf[ETH_HDR_LEN + IP_OFF_PROTO] = IPPROTO_TCP;
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_SRC ..][0..4], ip_src, .big);
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_DST ..][0..4], ip_dst, .big);
|
||||||
|
const tcp = ETH_HDR_LEN + IP_MIN_IHL;
|
||||||
|
std.mem.writeInt(u16, buf[tcp + TCP_OFF_SPORT ..][0..2], sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[tcp + TCP_OFF_DPORT ..][0..2], dport, .big);
|
||||||
|
std.mem.writeInt(u32, buf[tcp + TCP_OFF_SEQ ..][0..4], seq, .big);
|
||||||
|
std.mem.writeInt(u32, buf[tcp + TCP_OFF_ACK ..][0..4], ack, .big);
|
||||||
|
buf[tcp + TCP_OFF_FLAGS] = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buildIcmpUnreach(buf: *[128]u8, code: u8, inner_src: u32, inner_dst: u32, inner_sport: u16, inner_dport: u16, inner_seq: u32) usize {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x45;
|
||||||
|
buf[ETH_HDR_LEN + IP_OFF_PROTO] = IPPROTO_ICMP;
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_SRC ..][0..4], inner_dst, .big);
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_DST ..][0..4], inner_src, .big);
|
||||||
|
const icmp = ETH_HDR_LEN + IP_MIN_IHL;
|
||||||
|
buf[icmp + ICMP_OFF_TYPE] = ICMP_TYPE_DEST_UNREACH;
|
||||||
|
buf[icmp + ICMP_OFF_CODE] = code;
|
||||||
|
const inner = icmp + ICMP_HDR_LEN;
|
||||||
|
buf[inner] = 0x45;
|
||||||
|
buf[inner + IP_OFF_PROTO] = IPPROTO_TCP;
|
||||||
|
std.mem.writeInt(u32, buf[inner + IP_OFF_SRC ..][0..4], inner_src, .big);
|
||||||
|
std.mem.writeInt(u32, buf[inner + IP_OFF_DST ..][0..4], inner_dst, .big);
|
||||||
|
const inner_tcp = inner + IP_MIN_IHL;
|
||||||
|
std.mem.writeInt(u16, buf[inner_tcp + TCP_OFF_SPORT ..][0..2], inner_sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[inner_tcp + TCP_OFF_DPORT ..][0..2], inner_dport, .big);
|
||||||
|
std.mem.writeInt(u32, buf[inner_tcp + TCP_OFF_SEQ ..][0..4], inner_seq, .big);
|
||||||
|
return inner_tcp + ICMP_HDR_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated SYN-ACK classifies as open" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_seq = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0xCAFEBABE, our_seq +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
const r = classify(&f, ck).?;
|
||||||
|
try std.testing.expectEqual(State.open, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(their_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SYN-ACK with a wrong ack is rejected (anti-spoof)" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0xCAFEBABE, 0xDEADBEEF, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classify(&f, ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated RST/ACK classifies as closed" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_seq = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0, our_seq +% 1, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
const r = classify(&f, ck).?;
|
||||||
|
try std.testing.expectEqual(State.closed, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(their_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "RST/ACK with a wrong ack is rejected" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0, 0x11112222, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classify(&f, ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "bare RST without ACK is dropped as unvalidated" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0, 0, TCP_FLAG_RST);
|
||||||
|
try std.testing.expect(classify(&f, ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated ICMP dest-unreachable classifies as filtered" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_seq = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUnreach(&f, 3, our_ip, their_ip, our_port, their_port, our_seq);
|
||||||
|
const r = classify(f[0..len], ck).?;
|
||||||
|
try std.testing.expectEqual(State.filtered, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(their_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ICMP with a mismatched inner seq is rejected" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUnreach(&f, 3, our_ip, their_ip, our_port, their_port, 0x99999999);
|
||||||
|
try std.testing.expect(classify(f[0..len], ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ICMP with a non-filtered code is ignored" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_seq = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUnreach(&f, 4, our_ip, their_ip, our_port, their_port, our_seq);
|
||||||
|
try std.testing.expect(classify(f[0..len], ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "non-IPv4 ethertype is ignored" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&f, their_ip, our_ip, their_port, our_port, 0, 0, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
std.mem.writeInt(u16, f[ETH_OFF_TYPE..][0..2], 0x0806, .big);
|
||||||
|
try std.testing.expect(classify(&f, ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "runt frames return null instead of reading out of bounds" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var tiny = [_]u8{0} ** 20;
|
||||||
|
try std.testing.expect(classify(&tiny, ck) == null);
|
||||||
|
var empty = [_]u8{};
|
||||||
|
try std.testing.expect(classify(&empty, ck) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const udp_base: u16 = 40000;
|
||||||
|
const udp_span: u16 = 8192;
|
||||||
|
const udp_port: u16 = 53;
|
||||||
|
|
||||||
|
fn buildUdpReply(buf: *[42]u8, ip_src: u32, ip_dst: u32, sport: u16, dport: u16) void {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x45;
|
||||||
|
buf[ETH_HDR_LEN + IP_OFF_PROTO] = IPPROTO_UDP;
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_SRC ..][0..4], ip_src, .big);
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_DST ..][0..4], ip_dst, .big);
|
||||||
|
const udp = ETH_HDR_LEN + IP_MIN_IHL;
|
||||||
|
std.mem.writeInt(u16, buf[udp + UDP_OFF_SPORT ..][0..2], sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[udp + UDP_OFF_DPORT ..][0..2], dport, .big);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buildIcmpUdpUnreach(buf: *[128]u8, code: u8, inner_src: u32, inner_dst: u32, inner_sport: u16, inner_dport: u16) usize {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x45;
|
||||||
|
buf[ETH_HDR_LEN + IP_OFF_PROTO] = IPPROTO_ICMP;
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_SRC ..][0..4], inner_dst, .big);
|
||||||
|
std.mem.writeInt(u32, buf[ETH_HDR_LEN + IP_OFF_DST ..][0..4], inner_src, .big);
|
||||||
|
const icmp = ETH_HDR_LEN + IP_MIN_IHL;
|
||||||
|
buf[icmp + ICMP_OFF_TYPE] = ICMP_TYPE_DEST_UNREACH;
|
||||||
|
buf[icmp + ICMP_OFF_CODE] = code;
|
||||||
|
const inner = icmp + ICMP_HDR_LEN;
|
||||||
|
buf[inner] = 0x45;
|
||||||
|
buf[inner + IP_OFF_PROTO] = IPPROTO_UDP;
|
||||||
|
std.mem.writeInt(u32, buf[inner + IP_OFF_SRC ..][0..4], inner_src, .big);
|
||||||
|
std.mem.writeInt(u32, buf[inner + IP_OFF_DST ..][0..4], inner_dst, .big);
|
||||||
|
const inner_udp = inner + IP_MIN_IHL;
|
||||||
|
std.mem.writeInt(u16, buf[inner_udp + UDP_OFF_SPORT ..][0..2], inner_sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[inner_udp + UDP_OFF_DPORT ..][0..2], inner_dport, .big);
|
||||||
|
return inner_udp + INNER_UDP_MIN_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated UDP response classifies as open" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var f: [42]u8 = undefined;
|
||||||
|
buildUdpReply(&f, their_ip, our_ip, udp_port, our_src);
|
||||||
|
const r = classifyUdp(&f, ck, udp_base, udp_span).?;
|
||||||
|
try std.testing.expectEqual(State.open, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(udp_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "UDP response to a non-cookie destination port is rejected (anti-spoof)" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [42]u8 = undefined;
|
||||||
|
buildUdpReply(&f, their_ip, our_ip, udp_port, 12345);
|
||||||
|
try std.testing.expect(classifyUdp(&f, ck, udp_base, udp_span) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated ICMP port-unreachable (type 3 code 3) classifies as closed for UDP" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUdpUnreach(&f, 3, our_ip, their_ip, our_src, udp_port);
|
||||||
|
const r = classifyUdp(f[0..len], ck, udp_base, udp_span).?;
|
||||||
|
try std.testing.expectEqual(State.closed, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(udp_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated ICMP type 3 code 1 classifies as filtered for UDP" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUdpUnreach(&f, 1, our_ip, their_ip, our_src, udp_port);
|
||||||
|
const r = classifyUdp(f[0..len], ck, udp_base, udp_span).?;
|
||||||
|
try std.testing.expectEqual(State.filtered, r.state);
|
||||||
|
try std.testing.expectEqual(their_ip, r.addr.v4);
|
||||||
|
try std.testing.expectEqual(udp_port, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ICMP UDP-unreachable with a mismatched inner source port is rejected" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUdpUnreach(&f, 3, our_ip, their_ip, 9999, udp_port);
|
||||||
|
try std.testing.expect(classifyUdp(f[0..len], ck, udp_base, udp_span) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ICMP UDP-unreachable with a non-unreachable type is ignored" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUdpUnreach(&f, 3, our_ip, their_ip, our_src, udp_port);
|
||||||
|
f[ETH_HDR_LEN + IP_MIN_IHL + ICMP_OFF_TYPE] = 8;
|
||||||
|
try std.testing.expect(classifyUdp(f[0..len], ck, udp_base, udp_span) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "classifyUdp ignores non-IPv4 and runt frames" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [42]u8 = undefined;
|
||||||
|
buildUdpReply(&f, their_ip, our_ip, udp_port, ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span));
|
||||||
|
std.mem.writeInt(u16, f[ETH_OFF_TYPE..][0..2], 0x0806, .big);
|
||||||
|
try std.testing.expect(classifyUdp(&f, ck, udp_base, udp_span) == null);
|
||||||
|
var tiny = [_]u8{0} ** 20;
|
||||||
|
try std.testing.expect(classifyUdp(&tiny, ck, udp_base, udp_span) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the same ICMP code 3 is closed for UDP but filtered for a TCP SYN scan" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var f: [128]u8 = undefined;
|
||||||
|
const len = buildIcmpUdpUnreach(&f, 3, our_ip, their_ip, our_src, udp_port);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyUdp(f[0..len], ck, udp_base, udp_span).?.state);
|
||||||
|
try std.testing.expect(icmpCodeIsFilteredForSyn(3));
|
||||||
|
try std.testing.expect(!icmpCodeIsFilteredForUdp(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "classifier adapters route each frame to the right protocol path" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
|
||||||
|
const our_seq = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var tcp_f: [54]u8 = undefined;
|
||||||
|
buildTcpReply(&tcp_f, their_ip, our_ip, their_port, our_port, 0xCAFEBABE, our_seq +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
const tcp_clf = TcpClassifier{ .ck = ck };
|
||||||
|
try std.testing.expectEqual(State.open, tcp_clf.match(&tcp_f).?.state);
|
||||||
|
|
||||||
|
const our_src = ck.udpSrcPort(their_ip, udp_port, our_ip, udp_base, udp_span);
|
||||||
|
var udp_f: [42]u8 = undefined;
|
||||||
|
buildUdpReply(&udp_f, their_ip, our_ip, udp_port, our_src);
|
||||||
|
const udp_clf = UdpClassifier{ .ck = ck, .base = udp_base, .span = udp_span };
|
||||||
|
try std.testing.expectEqual(State.open, udp_clf.match(&udp_f).?.state);
|
||||||
|
try std.testing.expect(tcp_clf.match(&udp_f) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flagScanReply(buf: *[54]u8, seq: u32, ack: u32, flags: u8) void {
|
||||||
|
buildTcpReply(buf, their_ip, our_ip, their_port, our_port, seq, ack, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "FIN and Xmas scans classify a cookie-1 RST as closed and reject a wrong ack" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
|
||||||
|
for ([_]packet.ScanType{ .fin, .xmas }) |st| {
|
||||||
|
flagScanReply(&f, 0, cv +% 1, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyTcp(&f, ck, st).?.state);
|
||||||
|
flagScanReply(&f, 0, cv +% 5, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classifyTcp(&f, ck, st) == null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "NULL scan expects the RST ack to equal the cookie exactly (no sequence consumed)" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
flagScanReply(&f, 0, cv, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyTcp(&f, ck, .null_scan).?.state);
|
||||||
|
flagScanReply(&f, 0, cv +% 1, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classifyTcp(&f, ck, .null_scan) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a FIN scan does not classify a SYN-ACK (open ports stay silent)" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
flagScanReply(&f, 0xCAFEBABE, cv +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classifyTcp(&f, ck, .fin) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Maimon scan matches the RST sequence to the ack-field cookie" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
flagScanReply(&f, cv, 0, TCP_FLAG_RST);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyTcp(&f, ck, .maimon).?.state);
|
||||||
|
flagScanReply(&f, cv +% 3, 0, TCP_FLAG_RST);
|
||||||
|
try std.testing.expect(classifyTcp(&f, ck, .maimon) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ACK scan reports a validated RST as unfiltered, not open or closed" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
flagScanReply(&f, cv, 0, TCP_FLAG_RST);
|
||||||
|
try std.testing.expectEqual(State.unfiltered, classifyTcp(&f, ck, .ack).?.state);
|
||||||
|
flagScanReply(&f, cv +% 7, 0, TCP_FLAG_RST);
|
||||||
|
try std.testing.expect(classifyTcp(&f, ck, .ack) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Window scan reads the RST window: nonzero is open, zero is closed" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
const win_off = ETH_HDR_LEN + IP_MIN_IHL + TCP_OFF_WINDOW;
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
|
||||||
|
flagScanReply(&f, cv, 0, TCP_FLAG_RST);
|
||||||
|
std.mem.writeInt(u16, f[win_off..][0..2], 8192, .big);
|
||||||
|
try std.testing.expectEqual(State.open, classifyTcp(&f, ck, .window).?.state);
|
||||||
|
|
||||||
|
flagScanReply(&f, cv, 0, TCP_FLAG_RST);
|
||||||
|
std.mem.writeInt(u16, f[win_off..][0..2], 0, .big);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyTcp(&f, ck, .window).?.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the scan-type classifier adapter threads the mode into classifyTcp" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq(their_ip, their_port, our_ip, our_port);
|
||||||
|
var f: [54]u8 = undefined;
|
||||||
|
flagScanReply(&f, cv, 0, TCP_FLAG_RST);
|
||||||
|
const ack_clf = TcpClassifier{ .ck = ck, .scan = .ack };
|
||||||
|
try std.testing.expectEqual(State.unfiltered, ack_clf.match(&f).?.state);
|
||||||
|
const syn_clf = TcpClassifier{ .ck = ck };
|
||||||
|
try std.testing.expect(syn_clf.match(&f) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const our_ip6 = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 };
|
||||||
|
const their_ip6 = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x99 };
|
||||||
|
const our_port6: u16 = 40000;
|
||||||
|
const their_port6: u16 = 80;
|
||||||
|
|
||||||
|
fn buildTcp6Reply(buf: *[74]u8, src: [16]u8, dst: [16]u8, sport: u16, dport: u16, seq: u32, ack: u32, flags: u8) void {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV6, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x60;
|
||||||
|
buf[ETH_HDR_LEN + IP6_OFF_NEXT] = IPPROTO_TCP;
|
||||||
|
@memcpy(buf[ETH_HDR_LEN + IP6_OFF_SRC ..][0..16], &src);
|
||||||
|
@memcpy(buf[ETH_HDR_LEN + IP6_OFF_DST ..][0..16], &dst);
|
||||||
|
const tcp = ETH_HDR_LEN + IP6_HDR_LEN;
|
||||||
|
std.mem.writeInt(u16, buf[tcp + TCP_OFF_SPORT ..][0..2], sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[tcp + TCP_OFF_DPORT ..][0..2], dport, .big);
|
||||||
|
std.mem.writeInt(u32, buf[tcp + TCP_OFF_SEQ ..][0..4], seq, .big);
|
||||||
|
std.mem.writeInt(u32, buf[tcp + TCP_OFF_ACK ..][0..4], ack, .big);
|
||||||
|
buf[tcp + TCP_OFF_FLAGS] = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buildIcmp6Unreach(buf: *[122]u8, code: u8, inner_src: [16]u8, inner_dst: [16]u8, inner_sport: u16, inner_dport: u16, inner_seq: u32) usize {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV6, .big);
|
||||||
|
buf[ETH_HDR_LEN] = 0x60;
|
||||||
|
buf[ETH_HDR_LEN + IP6_OFF_NEXT] = IPPROTO_ICMPV6;
|
||||||
|
const icmp = ETH_HDR_LEN + IP6_HDR_LEN;
|
||||||
|
buf[icmp + ICMP_OFF_TYPE] = ICMP6_TYPE_DEST_UNREACH;
|
||||||
|
buf[icmp + ICMP_OFF_CODE] = code;
|
||||||
|
const inner = icmp + ICMP6_ERR_HDR_LEN;
|
||||||
|
buf[inner] = 0x60;
|
||||||
|
buf[inner + IP6_OFF_NEXT] = IPPROTO_TCP;
|
||||||
|
@memcpy(buf[inner + IP6_OFF_SRC ..][0..16], &inner_src);
|
||||||
|
@memcpy(buf[inner + IP6_OFF_DST ..][0..16], &inner_dst);
|
||||||
|
const inner_tcp = inner + IP6_HDR_LEN;
|
||||||
|
std.mem.writeInt(u16, buf[inner_tcp + TCP_OFF_SPORT ..][0..2], inner_sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[inner_tcp + TCP_OFF_DPORT ..][0..2], inner_dport, .big);
|
||||||
|
std.mem.writeInt(u32, buf[inner_tcp + TCP_OFF_SEQ ..][0..4], inner_seq, .big);
|
||||||
|
return inner_tcp + INNER_TCP_MIN_LEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated IPv6 SYN-ACK classifies as open with the responder address" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq6(their_ip6, their_port6, our_ip6, our_port6);
|
||||||
|
var f: [74]u8 = undefined;
|
||||||
|
buildTcp6Reply(&f, their_ip6, our_ip6, their_port6, our_port6, 0xCAFEBABE, cv +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
const r = classifyTcp6(&f, ck, .syn).?;
|
||||||
|
try std.testing.expectEqual(State.open, r.state);
|
||||||
|
try std.testing.expectEqualSlices(u8, &their_ip6, &r.addr.v6);
|
||||||
|
try std.testing.expectEqual(their_port6, r.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "IPv6 SYN-ACK with a wrong ack is rejected, RST/ACK is closed" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq6(their_ip6, their_port6, our_ip6, our_port6);
|
||||||
|
var f: [74]u8 = undefined;
|
||||||
|
buildTcp6Reply(&f, their_ip6, our_ip6, their_port6, our_port6, 0, 0xDEADBEEF, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expect(classifyTcp6(&f, ck, .syn) == null);
|
||||||
|
|
||||||
|
buildTcp6Reply(&f, their_ip6, our_ip6, their_port6, our_port6, 0, cv +% 1, TCP_FLAG_RST | TCP_FLAG_ACK);
|
||||||
|
try std.testing.expectEqual(State.closed, classifyTcp6(&f, ck, .syn).?.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validated ICMPv6 destination-unreachable classifies as filtered for the probed target" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq6(their_ip6, their_port6, our_ip6, our_port6);
|
||||||
|
var f: [122]u8 = undefined;
|
||||||
|
const len = buildIcmp6Unreach(&f, 4, our_ip6, their_ip6, our_port6, their_port6, cv);
|
||||||
|
const r = classifyTcp6(f[0..len], ck, .syn).?;
|
||||||
|
try std.testing.expectEqual(State.filtered, r.state);
|
||||||
|
try std.testing.expectEqualSlices(u8, &their_ip6, &r.addr.v6);
|
||||||
|
try std.testing.expectEqual(their_port6, r.port);
|
||||||
|
|
||||||
|
const bad = buildIcmp6Unreach(&f, 4, our_ip6, their_ip6, our_port6, their_port6, 0x99999999);
|
||||||
|
try std.testing.expect(classifyTcp6(f[0..bad], ck, .syn) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "classifyTcp6 ignores IPv4 frames and runt frames" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
var f: [74]u8 = undefined;
|
||||||
|
buildTcp6Reply(&f, their_ip6, our_ip6, their_port6, our_port6, 0, ck.seq6(their_ip6, their_port6, our_ip6, our_port6) +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
std.mem.writeInt(u16, f[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
try std.testing.expect(classifyTcp6(&f, ck, .syn) == null);
|
||||||
|
var tiny = [_]u8{0} ** 30;
|
||||||
|
try std.testing.expect(classifyTcp6(&tiny, ck, .syn) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the IPv6 classifier adapter threads the scan mode" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const cv = ck.seq6(their_ip6, their_port6, our_ip6, our_port6);
|
||||||
|
var f: [74]u8 = undefined;
|
||||||
|
buildTcp6Reply(&f, their_ip6, our_ip6, their_port6, our_port6, 0xCAFEBABE, cv +% 1, TCP_FLAG_SYN | TCP_FLAG_ACK);
|
||||||
|
const clf = TcpClassifier6{ .ck = ck };
|
||||||
|
try std.testing.expectEqual(State.open, clf.match(&f).?.state);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,104 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// cli.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const build_config = @import("build_config");
|
||||||
|
const output = @import("output");
|
||||||
|
|
||||||
|
const banner_art =
|
||||||
|
\\ ____ _ _
|
||||||
|
\\ |_ /(_) _ _ __ _ ___| | __ _
|
||||||
|
\\ / / | || ' \ / _` |/ -_) |/ _` |
|
||||||
|
\\ /___||_||_||_|\__, |\___|_|\__,_|
|
||||||
|
\\ |___/
|
||||||
|
;
|
||||||
|
|
||||||
|
pub fn printBanner(io: std.Io, env: *std.process.Environ.Map) !void {
|
||||||
|
var buf: [1024]u8 = undefined;
|
||||||
|
var fw = std.Io.File.stdout().writer(io, &buf);
|
||||||
|
const out = &fw.interface;
|
||||||
|
const level = output.envLevel(io, std.Io.File.stdout(), env, .auto);
|
||||||
|
try output.bannerWordmark(out, level, banner_art);
|
||||||
|
try out.print(" zingela {s} stateless mass scanner (Zig 0.16)\n\n", .{build_config.version});
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn printVersion(io: std.Io) !void {
|
||||||
|
var buf: [64]u8 = undefined;
|
||||||
|
var fw = std.Io.File.stdout().writer(io, &buf);
|
||||||
|
const out = &fw.interface;
|
||||||
|
try out.print("zingela {s}\n", .{build_config.version});
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn printHelp(io: std.Io, env: *std.process.Environ.Map) !void {
|
||||||
|
try printBanner(io, env);
|
||||||
|
var buf: [512]u8 = undefined;
|
||||||
|
var fw = std.Io.File.stdout().writer(io, &buf);
|
||||||
|
const out = &fw.interface;
|
||||||
|
try out.writeAll(
|
||||||
|
\\usage: zingela <command> [options]
|
||||||
|
\\
|
||||||
|
\\commands:
|
||||||
|
\\ smoke [ifname] send one hand-built SYN via AF_PACKET (default ifname: lo)
|
||||||
|
\\ tx [options] PACKET_TX_RING SYN blast over a target range (privileged)
|
||||||
|
\\ scan [options] SYN scan: transmit + classify replies open/closed/filtered (privileged)
|
||||||
|
\\ --version, -V print version
|
||||||
|
\\ --help, -h print this help
|
||||||
|
\\
|
||||||
|
\\tx / scan options:
|
||||||
|
\\ --target <cidr> target range, required (e.g. 10.0.0.0/24)
|
||||||
|
\\ --ports <list> comma-separated dst ports (default 80; --udp: 53,123,161)
|
||||||
|
\\ --rate <pps> token-bucket rate, packets per second (default 10000)
|
||||||
|
\\ --count <n> stop after n packets (default: every target once)
|
||||||
|
\\ --iface <name> egress interface (default lo)
|
||||||
|
\\ --src-ip <addr> source IPv4 (default: resolved from --iface)
|
||||||
|
\\ --src-port <n> source port; UDP uses it as the cookie-range base (default 40000)
|
||||||
|
\\ --gw-mac <mac> gateway/dst MAC aa:bb:cc:dd:ee:ff (default 00:..:00)
|
||||||
|
\\ --seed <n> permutation seed (default: per-scan CSPRNG)
|
||||||
|
\\ --backend <b> TX path: auto | xdp | afpacket | connect (default auto;
|
||||||
|
\\ xdp needs a -Dxdp build; connect = unprivileged TCP connect scan)
|
||||||
|
\\
|
||||||
|
\\scan-only options:
|
||||||
|
\\ --udp UDP scan: per-protocol payloads, ICMP type3/code3 = closed,
|
||||||
|
\\ silent ports reported honestly as open|filtered
|
||||||
|
\\ --banners SYN-scan only: phase-2 service/banner grab on open ports
|
||||||
|
\\ (NULL probe + HTTP GET, TLS detected not decrypted, no JA4);
|
||||||
|
\\ auto-installs a scoped RST-drop so the grab survives the kernel
|
||||||
|
\\ --wait <ms> receive drain window after transmit (default 2000)
|
||||||
|
\\ --json emit NDJSON results to stdout (visuals go to stderr)
|
||||||
|
\\ --color <when> auto | always | never (default auto)
|
||||||
|
\\
|
||||||
|
\\connect-scan options (--backend connect / --connect; no CAP_NET_RAW needed):
|
||||||
|
\\ --connect TCP connect() scan via the OS stack (open/closed/filtered)
|
||||||
|
\\ --concurrency <n> concurrent connect workers (default 128)
|
||||||
|
\\ --connect-timeout <ms> per-connect timeout, filtered on no reply (default 3000)
|
||||||
|
\\
|
||||||
|
\\IPv6 scan (a --target with ':' is IPv6; raw stateless SYN, or add --connect):
|
||||||
|
\\ --target <cidr6> bounded IPv6 prefix, e.g. 2001:db8::/112 (::/0 is refused)
|
||||||
|
\\ --src-ip6 <addr> source IPv6 (default: resolved from --iface via /proc/net/if_inet6)
|
||||||
|
\\ --gw-ip6 <addr> next-hop IPv6 to resolve via NDP (default: the iface v6 default route)
|
||||||
|
\\ --gw-mac <mac> skip NDP and stamp this next-hop MAC directly
|
||||||
|
\\ --max-hosts <n> host-address cap per IPv6 prefix (default 1048576)
|
||||||
|
\\
|
||||||
|
\\stealth / evasion (tx + scan; every flag requires --authorized-scan):
|
||||||
|
\\ --authorized-scan confirm you are authorized to scan the target
|
||||||
|
\\ --os-template <os> SYN fingerprint none|masscan|linux|windows|macos
|
||||||
|
\\ --scan-type <t> syn|fin|null|xmas|maimon|ack|window (default syn)
|
||||||
|
\\ --jitter <mode> poisson | none: exponential inter-packet timing
|
||||||
|
\\ --source-port-rotation vary the source port per probe (cookie still matches)
|
||||||
|
\\ --decoys <list> spoofed decoys ip1,ip2,RND:N (real probe always sent)
|
||||||
|
\\ --suppress-rst drop our own kernel RSTs on the scan port range
|
||||||
|
\\ (idle-scan, fragmentation, TTL, and MAC/source-route spoofing are deliberately
|
||||||
|
\\ omitted as obsolete in 2026; run with --authorized-scan for the rationale)
|
||||||
|
\\
|
||||||
|
\\authorized use only. responsible default rate; needs CAP_NET_RAW
|
||||||
|
\\(grant once: sudo setcap cap_net_raw,cap_net_admin=eip ./zig-out/bin/zingela)
|
||||||
|
\\
|
||||||
|
);
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
test "version string is non-empty" {
|
||||||
|
try std.testing.expect(build_config.version.len > 0);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,402 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// connect.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const mem = std.mem;
|
||||||
|
const net = std.Io.net;
|
||||||
|
const classify = @import("classify");
|
||||||
|
const packet = @import("packet");
|
||||||
|
const targets = @import("targets");
|
||||||
|
const ratelimit = @import("ratelimit");
|
||||||
|
const output = @import("output");
|
||||||
|
|
||||||
|
pub const Result = classify.Result;
|
||||||
|
pub const State = classify.State;
|
||||||
|
|
||||||
|
pub const default_concurrency: usize = 128;
|
||||||
|
pub const default_timeout_ms: u64 = 3000;
|
||||||
|
pub const min_concurrency: usize = 1;
|
||||||
|
pub const max_concurrency: usize = 1024;
|
||||||
|
|
||||||
|
const NS_PER_MS: u64 = 1_000_000;
|
||||||
|
const NS_PER_SEC: u64 = 1_000_000_000;
|
||||||
|
const fd_retry_limit: u32 = 4;
|
||||||
|
const fd_retry_sleep_ns: u64 = 5 * NS_PER_MS;
|
||||||
|
const render_tick_interactive_ns: u64 = 125 * NS_PER_MS;
|
||||||
|
const render_tick_plain_ns: u64 = 1000 * NS_PER_MS;
|
||||||
|
const drain_tick_ns: u64 = 40 * NS_PER_MS;
|
||||||
|
|
||||||
|
fn monoNow() u64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(u64, @intCast(ts.sec)) * NS_PER_SEC + @as(u64, @intCast(ts.nsec));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sleepNs(ns: u64) void {
|
||||||
|
const ts = linux.timespec{ .sec = @intCast(ns / NS_PER_SEC), .nsec = @intCast(ns % NS_PER_SEC) };
|
||||||
|
_ = linux.nanosleep(&ts, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Target = struct {
|
||||||
|
addr: packet.Addr,
|
||||||
|
port: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Source = union(enum) {
|
||||||
|
v4: *targets.Engine,
|
||||||
|
v6: *targets.Engine6,
|
||||||
|
|
||||||
|
fn next(self: Source) ?Target {
|
||||||
|
switch (self) {
|
||||||
|
.v4 => |eng| {
|
||||||
|
const t = eng.next() orelse return null;
|
||||||
|
return .{ .addr = .{ .v4 = t.ip }, .port = t.port };
|
||||||
|
},
|
||||||
|
.v6 => |eng| {
|
||||||
|
const t = eng.next() orelse return null;
|
||||||
|
return .{ .addr = .{ .v6 = t.addr }, .port = t.port };
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Probe = union(enum) {
|
||||||
|
done: State,
|
||||||
|
retry,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn classifySoError(so_err: u32) State {
|
||||||
|
return switch (so_err) {
|
||||||
|
0 => .open,
|
||||||
|
@intFromEnum(linux.E.CONNREFUSED) => .closed,
|
||||||
|
@intFromEnum(linux.E.HOSTUNREACH),
|
||||||
|
@intFromEnum(linux.E.NETUNREACH),
|
||||||
|
@intFromEnum(linux.E.TIMEDOUT),
|
||||||
|
@intFromEnum(linux.E.ACCES),
|
||||||
|
@intFromEnum(linux.E.PERM),
|
||||||
|
@intFromEnum(linux.E.CONNRESET),
|
||||||
|
=> .filtered,
|
||||||
|
else => .filtered,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connectOnce(addr: packet.Addr, port: u16, timeout_ns: u64) Probe {
|
||||||
|
const domain: u32 = switch (addr) {
|
||||||
|
.v4 => linux.AF.INET,
|
||||||
|
.v6 => linux.AF.INET6,
|
||||||
|
};
|
||||||
|
const rc_sock = linux.socket(domain, linux.SOCK.STREAM | linux.SOCK.NONBLOCK | linux.SOCK.CLOEXEC, 0);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.MFILE, .NFILE, .NOBUFS, .NOMEM => return .retry,
|
||||||
|
else => return .{ .done = .filtered },
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
defer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var v4sa: linux.sockaddr.in = undefined;
|
||||||
|
var v6sa: linux.sockaddr.in6 = undefined;
|
||||||
|
const sa_ptr: *const anyopaque = switch (addr) {
|
||||||
|
.v4 => |ip| blk: {
|
||||||
|
v4sa = .{ .port = mem.nativeToBig(u16, port), .addr = mem.nativeToBig(u32, ip) };
|
||||||
|
break :blk @ptrCast(&v4sa);
|
||||||
|
},
|
||||||
|
.v6 => |b| blk: {
|
||||||
|
v6sa = .{ .port = mem.nativeToBig(u16, port), .flowinfo = 0, .addr = b, .scope_id = 0 };
|
||||||
|
break :blk @ptrCast(&v6sa);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const sa_len: linux.socklen_t = switch (addr) {
|
||||||
|
.v4 => @sizeOf(linux.sockaddr.in),
|
||||||
|
.v6 => @sizeOf(linux.sockaddr.in6),
|
||||||
|
};
|
||||||
|
|
||||||
|
switch (linux.errno(linux.connect(fd, sa_ptr, sa_len))) {
|
||||||
|
.SUCCESS, .ISCONN => return .{ .done = .open },
|
||||||
|
.INPROGRESS, .INTR, .AGAIN => {},
|
||||||
|
.CONNREFUSED => return .{ .done = .closed },
|
||||||
|
.MFILE, .NFILE, .NOBUFS, .NOMEM => return .retry,
|
||||||
|
else => return .{ .done = .filtered },
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeout_ms: i32 = @intCast(@min(timeout_ns / NS_PER_MS, @as(u64, std.math.maxInt(i32))));
|
||||||
|
var pfd = [_]linux.pollfd{.{ .fd = fd, .events = linux.POLL.OUT, .revents = 0 }};
|
||||||
|
while (true) {
|
||||||
|
const pr = linux.poll(&pfd, 1, timeout_ms);
|
||||||
|
switch (linux.errno(pr)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return .{ .done = .filtered },
|
||||||
|
}
|
||||||
|
if (pr == 0) return .{ .done = .filtered };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var so_err: u32 = 0;
|
||||||
|
var so_len: linux.socklen_t = @sizeOf(u32);
|
||||||
|
_ = linux.getsockopt(fd, linux.SOL.SOCKET, linux.SO.ERROR, @ptrCast(&so_err), &so_len);
|
||||||
|
return .{ .done = classifySoError(so_err) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn connectProbe(addr: packet.Addr, port: u16, timeout_ns: u64) State {
|
||||||
|
var attempt: u32 = 0;
|
||||||
|
while (true) {
|
||||||
|
switch (connectOnce(addr, port, timeout_ns)) {
|
||||||
|
.done => |st| return st,
|
||||||
|
.retry => {
|
||||||
|
attempt += 1;
|
||||||
|
if (attempt >= fd_retry_limit) return .filtered;
|
||||||
|
sleepNs(fd_retry_sleep_ns);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Dispenser = struct {
|
||||||
|
mutex: std.Io.Mutex = .init,
|
||||||
|
source: Source,
|
||||||
|
bucket: ratelimit.TokenBucket,
|
||||||
|
remaining: u64,
|
||||||
|
exhausted: bool = false,
|
||||||
|
|
||||||
|
fn next(self: *Dispenser, io: std.Io) ?Target {
|
||||||
|
while (true) {
|
||||||
|
self.mutex.lockUncancelable(io);
|
||||||
|
if (self.exhausted or self.remaining == 0) {
|
||||||
|
self.mutex.unlock(io);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const granted = self.bucket.takeBatch(monoNow(), 1);
|
||||||
|
if (granted == 0) {
|
||||||
|
const wait = self.bucket.step_ns;
|
||||||
|
self.mutex.unlock(io);
|
||||||
|
std.Io.sleep(io, .{ .nanoseconds = @intCast(wait) }, .awake) catch {};
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const t = self.source.next() orelse {
|
||||||
|
self.exhausted = true;
|
||||||
|
self.mutex.unlock(io);
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
self.remaining -= 1;
|
||||||
|
self.mutex.unlock(io);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Collected = struct {
|
||||||
|
mutex: std.Io.Mutex = .init,
|
||||||
|
list: std.ArrayList(Result) = .empty,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
|
||||||
|
fn push(self: *Collected, io: std.Io, r: Result) void {
|
||||||
|
self.mutex.lockUncancelable(io);
|
||||||
|
defer self.mutex.unlock(io);
|
||||||
|
self.list.append(self.allocator, r) catch {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn worker(
|
||||||
|
io: std.Io,
|
||||||
|
disp: *Dispenser,
|
||||||
|
timeout_ns: u64,
|
||||||
|
collected: *Collected,
|
||||||
|
stats: *output.Stats,
|
||||||
|
remaining: *std.atomic.Value(usize),
|
||||||
|
) void {
|
||||||
|
while (disp.next(io)) |t| {
|
||||||
|
const st = connectProbe(t.addr, t.port, timeout_ns);
|
||||||
|
_ = stats.sent.v.fetchAdd(1, .monotonic);
|
||||||
|
stats.record(st);
|
||||||
|
collected.push(io, .{ .addr = t.addr, .port = t.port, .state = st });
|
||||||
|
}
|
||||||
|
_ = remaining.fetchSub(1, .release);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Params = struct {
|
||||||
|
source: Source,
|
||||||
|
count: u64,
|
||||||
|
total: u64,
|
||||||
|
rate: u64,
|
||||||
|
concurrency: usize,
|
||||||
|
timeout_ns: u64,
|
||||||
|
out_level: output.ColorLevel,
|
||||||
|
err_level: output.ColorLevel,
|
||||||
|
interactive: bool,
|
||||||
|
json: bool,
|
||||||
|
target_text: []const u8,
|
||||||
|
iface: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn drainNew(io: std.Io, collected: *Collected, cursor: *usize, json_out: ?*std.Io.Writer) void {
|
||||||
|
collected.mutex.lockUncancelable(io);
|
||||||
|
const n = collected.list.items.len;
|
||||||
|
if (json_out) |w| {
|
||||||
|
while (cursor.* < n) : (cursor.* += 1) {
|
||||||
|
output.emitJson(w, collected.list.items[cursor.*], "tcp") catch {};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cursor.* = n;
|
||||||
|
}
|
||||||
|
collected.mutex.unlock(io);
|
||||||
|
if (json_out) |w| w.flush() catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(gpa: std.mem.Allocator, p: Params) !void {
|
||||||
|
const concurrency = std.math.clamp(p.concurrency, min_concurrency, max_concurrency);
|
||||||
|
|
||||||
|
var threaded = std.Io.Threaded.init(gpa, .{
|
||||||
|
.async_limit = .limited(concurrency + 1),
|
||||||
|
.concurrent_limit = .limited(concurrency + 1),
|
||||||
|
});
|
||||||
|
defer threaded.deinit();
|
||||||
|
const io = threaded.io();
|
||||||
|
|
||||||
|
var obuf: [4096]u8 = undefined;
|
||||||
|
var ow = std.Io.File.stdout().writer(io, &obuf);
|
||||||
|
const out = &ow.interface;
|
||||||
|
var ebuf: [4096]u8 = undefined;
|
||||||
|
var ew = std.Io.File.stderr().writer(io, &ebuf);
|
||||||
|
const derr = &ew.interface;
|
||||||
|
|
||||||
|
var bucket = ratelimit.TokenBucket.init(p.rate, p.rate);
|
||||||
|
bucket.prime(monoNow());
|
||||||
|
|
||||||
|
var disp = Dispenser{ .source = p.source, .bucket = bucket, .remaining = p.count };
|
||||||
|
var collected = Collected{ .allocator = gpa };
|
||||||
|
defer collected.list.deinit(gpa);
|
||||||
|
var stats: output.Stats = .{};
|
||||||
|
var remaining = std.atomic.Value(usize).init(concurrency);
|
||||||
|
const json_out: ?*std.Io.Writer = if (p.json) out else null;
|
||||||
|
|
||||||
|
try derr.print("zingela connect scan target {s} iface {s} rate {d} pps concurrency {d} timeout {d}ms\n", .{
|
||||||
|
p.target_text, p.iface, p.rate, concurrency, p.timeout_ns / NS_PER_MS,
|
||||||
|
});
|
||||||
|
try derr.flush();
|
||||||
|
|
||||||
|
const t0 = monoNow();
|
||||||
|
|
||||||
|
var group: std.Io.Group = .init;
|
||||||
|
defer group.cancel(io);
|
||||||
|
var spawned: usize = 0;
|
||||||
|
while (spawned < concurrency) : (spawned += 1) {
|
||||||
|
group.async(io, worker, .{ io, &disp, p.timeout_ns, &collected, &stats, &remaining });
|
||||||
|
}
|
||||||
|
|
||||||
|
var dash = output.Dashboard.init(p.err_level, p.interactive, p.total);
|
||||||
|
const render_interval_ns: u64 = if (p.interactive) render_tick_interactive_ns else render_tick_plain_ns;
|
||||||
|
var cursor: usize = 0;
|
||||||
|
var last_render: u64 = 0;
|
||||||
|
|
||||||
|
while (remaining.load(.acquire) > 0) {
|
||||||
|
drainNew(io, &collected, &cursor, json_out);
|
||||||
|
const now = monoNow();
|
||||||
|
if (last_render == 0 or now -| last_render >= render_interval_ns) {
|
||||||
|
dash.render(derr, &stats, now -| t0) catch {};
|
||||||
|
last_render = now;
|
||||||
|
}
|
||||||
|
std.Io.sleep(io, .{ .nanoseconds = @intCast(drain_tick_ns) }, .awake) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
group.await(io) catch {};
|
||||||
|
drainNew(io, &collected, &cursor, json_out);
|
||||||
|
dash.render(derr, &stats, monoNow() -| t0) catch {};
|
||||||
|
|
||||||
|
var open_n: u64 = 0;
|
||||||
|
var closed_n: u64 = 0;
|
||||||
|
var filtered_n: u64 = 0;
|
||||||
|
for (collected.list.items) |r| switch (r.state) {
|
||||||
|
.open => open_n += 1,
|
||||||
|
.closed => closed_n += 1,
|
||||||
|
.filtered => filtered_n += 1,
|
||||||
|
.unfiltered => {},
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!p.json) {
|
||||||
|
if (collected.list.items.len > 0) {
|
||||||
|
std.mem.sort(Result, collected.list.items, {}, output.ipPortLess);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
try output.renderTable(out, p.out_level, collected.list.items);
|
||||||
|
try out.flush();
|
||||||
|
} else {
|
||||||
|
try derr.writeAll(" no hosts responded\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed_s = @as(f64, @floatFromInt(monoNow() - t0)) / @as(f64, @floatFromInt(NS_PER_SEC));
|
||||||
|
try derr.writeByte('\n');
|
||||||
|
try output.renderSummary(derr, p.err_level, stats.sent.v.load(.monotonic), "CONNECT", p.iface, elapsed_s, open_n, closed_n, filtered_n, 0, 0);
|
||||||
|
try derr.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- tests ----
|
||||||
|
|
||||||
|
test "classifySoError maps SO_ERROR values to scan states" {
|
||||||
|
try std.testing.expectEqual(State.open, classifySoError(0));
|
||||||
|
try std.testing.expectEqual(State.closed, classifySoError(@intFromEnum(linux.E.CONNREFUSED)));
|
||||||
|
try std.testing.expectEqual(State.filtered, classifySoError(@intFromEnum(linux.E.HOSTUNREACH)));
|
||||||
|
try std.testing.expectEqual(State.filtered, classifySoError(@intFromEnum(linux.E.TIMEDOUT)));
|
||||||
|
try std.testing.expectEqual(State.filtered, classifySoError(9999));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "connectOnce surfaces a retry for fd exhaustion but a real state otherwise" {
|
||||||
|
try std.testing.expect(connectOnce(.{ .v4 = 0x7f000001 }, 1, 200 * NS_PER_MS) == .done);
|
||||||
|
}
|
||||||
|
|
||||||
|
const FoundListener = struct { server: net.Server, port: u16 };
|
||||||
|
|
||||||
|
fn bindFreeLoopback(io: std.Io, start: u16) ?FoundListener {
|
||||||
|
var port: u16 = start;
|
||||||
|
while (port < start +% 200) : (port += 1) {
|
||||||
|
var la: net.IpAddress = .{ .ip4 = net.Ip4Address.loopback(port) };
|
||||||
|
if (net.IpAddress.listen(&la, io, .{ .reuse_address = true })) |s| {
|
||||||
|
return .{ .server = s, .port = port };
|
||||||
|
} else |_| {}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "connectProbe classifies a live loopback listener as open and a released port as closed" {
|
||||||
|
var threaded = std.Io.Threaded.init(std.testing.allocator, .{});
|
||||||
|
defer threaded.deinit();
|
||||||
|
const io = threaded.io();
|
||||||
|
|
||||||
|
const live = bindFreeLoopback(io, 39_211) orelse return error.SkipZigTest;
|
||||||
|
var live_server = live.server;
|
||||||
|
defer live_server.deinit(io);
|
||||||
|
|
||||||
|
var dead = bindFreeLoopback(io, live.port + 1) orelse return error.SkipZigTest;
|
||||||
|
const dead_port = dead.port;
|
||||||
|
dead.server.deinit(io);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(State.open, connectProbe(.{ .v4 = 0x7f000001 }, live.port, 2 * NS_PER_SEC));
|
||||||
|
try std.testing.expectEqual(State.closed, connectProbe(.{ .v4 = 0x7f000001 }, dead_port, 2 * NS_PER_SEC));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Dispenser stops at the count cap and hands out distinct targets under the token bucket" {
|
||||||
|
var threaded = std.Io.Threaded.init(std.testing.allocator, .{});
|
||||||
|
defer threaded.deinit();
|
||||||
|
const io = threaded.io();
|
||||||
|
|
||||||
|
const cidr = try targets.parseCidr("8.8.8.0/28");
|
||||||
|
const ports = [_]u16{ 80, 443 };
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &.{cidr}, &ports, 0xABCDEF);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
var bucket = ratelimit.TokenBucket.init(1_000_000, 1_000_000);
|
||||||
|
bucket.prime(monoNow());
|
||||||
|
var disp = Dispenser{ .source = .{ .v4 = &eng }, .bucket = bucket, .remaining = 5 };
|
||||||
|
|
||||||
|
var seen = std.AutoHashMap(u64, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
var n: usize = 0;
|
||||||
|
while (disp.next(io)) |t| {
|
||||||
|
const key = (@as(u64, t.addr.v4) << 16) | t.port;
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, 5), n);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,156 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// cookie.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const Cookie = struct {
|
||||||
|
key: [16]u8,
|
||||||
|
|
||||||
|
pub fn init(key: [16]u8) Cookie {
|
||||||
|
return .{ .key = key };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn random(io: std.Io) !Cookie {
|
||||||
|
var key: [16]u8 = undefined;
|
||||||
|
try io.randomSecure(&key);
|
||||||
|
return .{ .key = key };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate(self: Cookie, ip_them: u32, port_them: u16, ip_me: u32, port_me: u16) u64 {
|
||||||
|
var data: [12]u8 = undefined;
|
||||||
|
std.mem.writeInt(u32, data[0..4], ip_them, .big);
|
||||||
|
std.mem.writeInt(u16, data[4..6], port_them, .big);
|
||||||
|
std.mem.writeInt(u32, data[6..10], ip_me, .big);
|
||||||
|
std.mem.writeInt(u16, data[10..12], port_me, .big);
|
||||||
|
return std.hash.SipHash64(2, 4).toInt(&data, &self.key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seq(self: Cookie, ip_them: u32, port_them: u16, ip_me: u32, port_me: u16) u32 {
|
||||||
|
return @truncate(self.generate(ip_them, port_them, ip_me, port_me));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validateSynAck(self: Cookie, ack: u32, ip_them: u32, port_them: u16, ip_me: u32, port_me: u16) bool {
|
||||||
|
return ack == self.seq(ip_them, port_them, ip_me, port_me) +% 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn udpSrcPort(self: Cookie, ip_them: u32, port_them: u16, ip_me: u32, base: u16, span: u16) u16 {
|
||||||
|
const s: u32 = if (span == 0) 1 else span;
|
||||||
|
const off: u32 = @intCast(self.generate(ip_them, port_them, ip_me, 0) % s);
|
||||||
|
return @intCast((@as(u32, base) + off) & 0xffff);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate6(self: Cookie, ip_them: [16]u8, port_them: u16, ip_me: [16]u8, port_me: u16) u64 {
|
||||||
|
var data: [36]u8 = undefined;
|
||||||
|
@memcpy(data[0..16], &ip_them);
|
||||||
|
std.mem.writeInt(u16, data[16..18], port_them, .big);
|
||||||
|
@memcpy(data[18..34], &ip_me);
|
||||||
|
std.mem.writeInt(u16, data[34..36], port_me, .big);
|
||||||
|
return std.hash.SipHash64(2, 4).toInt(&data, &self.key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seq6(self: Cookie, ip_them: [16]u8, port_them: u16, ip_me: [16]u8, port_me: u16) u32 {
|
||||||
|
return @truncate(self.generate6(ip_them, port_them, ip_me, port_me));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validateSynAck6(self: Cookie, ack: u32, ip_them: [16]u8, port_them: u16, ip_me: [16]u8, port_me: u16) bool {
|
||||||
|
return ack == self.seq6(ip_them, port_them, ip_me, port_me) +% 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
test "SipHash64(2,4) reproduces the reference empty-message vector" {
|
||||||
|
try std.testing.expectEqual(
|
||||||
|
@as(u64, 0x726fdb47dd0e0e31),
|
||||||
|
std.hash.SipHash64(2, 4).toInt("", &test_key),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "cookie is deterministic for a fixed key + 4-tuple (golden KAT)" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const a = c.generate(0x0a000001, 443, 0xc0a80002, 51000);
|
||||||
|
const b = c.generate(0x0a000001, 443, 0xc0a80002, 51000);
|
||||||
|
try std.testing.expectEqual(a, b);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0x559a4e08e1deb9a7), a);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seq is the low 32 bits of the cookie" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const full = c.generate(0x0a000001, 443, 0xc0a80002, 51000);
|
||||||
|
try std.testing.expectEqual(@as(u32, @truncate(full)), c.seq(0x0a000001, 443, 0xc0a80002, 51000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validateSynAck accepts ack == seq + 1 and rejects others" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const s = c.seq(0x0a000001, 443, 0xc0a80002, 51000);
|
||||||
|
try std.testing.expect(c.validateSynAck(s +% 1, 0x0a000001, 443, 0xc0a80002, 51000));
|
||||||
|
try std.testing.expect(!c.validateSynAck(s, 0x0a000001, 443, 0xc0a80002, 51000));
|
||||||
|
try std.testing.expect(!c.validateSynAck(s +% 2, 0x0a000001, 443, 0xc0a80002, 51000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "validateSynAck wraps at the u32 boundary" {
|
||||||
|
const seq_max: u32 = 0xFFFFFFFF;
|
||||||
|
try std.testing.expectEqual(@as(u32, 0), seq_max +% 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "udpSrcPort is deterministic and stays inside [base, base+span)" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const base: u16 = 40000;
|
||||||
|
const span: u16 = 8192;
|
||||||
|
const a = c.udpSrcPort(0x08080808, 53, 0x0a000001, base, span);
|
||||||
|
const b = c.udpSrcPort(0x08080808, 53, 0x0a000001, base, span);
|
||||||
|
try std.testing.expectEqual(a, b);
|
||||||
|
try std.testing.expect(a >= base and a < base + span);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "udpSrcPort excludes port_me from the tuple so RX can recompute it" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const full = c.generate(0x08080808, 53, 0x0a000001, 0);
|
||||||
|
const want: u16 = @intCast((@as(u32, 40000) + @as(u32, @intCast(full % 8192))) & 0xffff);
|
||||||
|
try std.testing.expectEqual(want, c.udpSrcPort(0x08080808, 53, 0x0a000001, 40000, 8192));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "udpSrcPort separates distinct targets" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const p53 = c.udpSrcPort(0x08080808, 53, 0x0a000001, 40000, 8192);
|
||||||
|
const p123 = c.udpSrcPort(0x08080808, 123, 0x0a000001, 40000, 8192);
|
||||||
|
try std.testing.expect(p53 != p123);
|
||||||
|
}
|
||||||
|
|
||||||
|
const v6_them = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x11 };
|
||||||
|
const v6_me = [16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x22 };
|
||||||
|
|
||||||
|
test "generate6 is deterministic for a fixed key + IPv6 4-tuple" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const a = c.generate6(v6_them, 443, v6_me, 51000);
|
||||||
|
const b = c.generate6(v6_them, 443, v6_me, 51000);
|
||||||
|
try std.testing.expectEqual(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seq6 is the low 32 bits and validateSynAck6 accepts seq+1 with u32 wrap" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const full = c.generate6(v6_them, 443, v6_me, 51000);
|
||||||
|
const s = c.seq6(v6_them, 443, v6_me, 51000);
|
||||||
|
try std.testing.expectEqual(@as(u32, @truncate(full)), s);
|
||||||
|
try std.testing.expect(c.validateSynAck6(s +% 1, v6_them, 443, v6_me, 51000));
|
||||||
|
try std.testing.expect(!c.validateSynAck6(s, v6_them, 443, v6_me, 51000));
|
||||||
|
try std.testing.expect(!c.validateSynAck6(s +% 2, v6_them, 443, v6_me, 51000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "generate6 separates distinct addresses and ports" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
var other = v6_them;
|
||||||
|
other[15] = 0x12;
|
||||||
|
try std.testing.expect(c.generate6(v6_them, 443, v6_me, 51000) != c.generate6(other, 443, v6_me, 51000));
|
||||||
|
try std.testing.expect(c.generate6(v6_them, 443, v6_me, 51000) != c.generate6(v6_them, 80, v6_me, 51000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "v4 and v6 cookies over analogous tuples do not collide" {
|
||||||
|
const c = Cookie.init(test_key);
|
||||||
|
const v4 = c.generate(0x0a000001, 443, 0xc0a80002, 51000);
|
||||||
|
const v6 = c.generate6(v6_them, 443, v6_me, 51000);
|
||||||
|
try std.testing.expect(v4 != v6);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,122 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// dedup.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const EMPTY: u64 = std.math.maxInt(u64);
|
||||||
|
const LOAD_NUM: usize = 7;
|
||||||
|
const LOAD_DEN: usize = 10;
|
||||||
|
const FMIX64_MUL: u64 = 0xff51afd7ed558ccd;
|
||||||
|
const FMIX64_SHIFT: u6 = 33;
|
||||||
|
|
||||||
|
pub const Dedup = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
slots: []u64,
|
||||||
|
mask: usize,
|
||||||
|
len: usize,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator, capacity_pow2: usize) !Dedup {
|
||||||
|
std.debug.assert(capacity_pow2 >= 2 and std.math.isPowerOfTwo(capacity_pow2));
|
||||||
|
const slots = try allocator.alloc(u64, capacity_pow2);
|
||||||
|
@memset(slots, EMPTY);
|
||||||
|
return .{ .allocator = allocator, .slots = slots, .mask = capacity_pow2 - 1, .len = 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Dedup) void {
|
||||||
|
self.allocator.free(self.slots);
|
||||||
|
self.* = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mix(k: u64) u64 {
|
||||||
|
var x = k;
|
||||||
|
x ^= x >> FMIX64_SHIFT;
|
||||||
|
x *%= FMIX64_MUL;
|
||||||
|
x ^= x >> FMIX64_SHIFT;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PlaceResult = enum { inserted, duplicate, full };
|
||||||
|
|
||||||
|
fn place(slots: []u64, mask: usize, k: u64) PlaceResult {
|
||||||
|
var i: usize = @intCast(mix(k) & mask);
|
||||||
|
var probes: usize = 0;
|
||||||
|
while (probes <= mask) : (probes += 1) {
|
||||||
|
if (slots[i] == EMPTY) {
|
||||||
|
slots[i] = k;
|
||||||
|
return .inserted;
|
||||||
|
}
|
||||||
|
if (slots[i] == k) return .duplicate;
|
||||||
|
i = (i + 1) & mask;
|
||||||
|
}
|
||||||
|
return .full;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn grow(self: *Dedup) !void {
|
||||||
|
const new_cap = (self.mask + 1) * 2;
|
||||||
|
const new_slots = try self.allocator.alloc(u64, new_cap);
|
||||||
|
@memset(new_slots, EMPTY);
|
||||||
|
const new_mask = new_cap - 1;
|
||||||
|
for (self.slots) |s| {
|
||||||
|
if (s != EMPTY) _ = place(new_slots, new_mask, s);
|
||||||
|
}
|
||||||
|
self.allocator.free(self.slots);
|
||||||
|
self.slots = new_slots;
|
||||||
|
self.mask = new_mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(self: *Dedup, k: u64) bool {
|
||||||
|
if ((self.len + 1) * LOAD_DEN > (self.mask + 1) * LOAD_NUM) {
|
||||||
|
self.grow() catch {};
|
||||||
|
}
|
||||||
|
return switch (place(self.slots, self.mask, k)) {
|
||||||
|
.inserted => blk: {
|
||||||
|
self.len += 1;
|
||||||
|
break :blk true;
|
||||||
|
},
|
||||||
|
.duplicate => false,
|
||||||
|
.full => true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn key(ip: u32, port: u16) u64 {
|
||||||
|
return (@as(u64, ip) << 16) | port;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "first insert is new, second of same key is duplicate" {
|
||||||
|
var d = try Dedup.init(std.testing.allocator, 16);
|
||||||
|
defer d.deinit();
|
||||||
|
try std.testing.expect(d.insert(key(0x08080808, 80)));
|
||||||
|
try std.testing.expect(!d.insert(key(0x08080808, 80)));
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), d.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "same ip different ports are distinct" {
|
||||||
|
var d = try Dedup.init(std.testing.allocator, 16);
|
||||||
|
defer d.deinit();
|
||||||
|
try std.testing.expect(d.insert(key(0x08080808, 80)));
|
||||||
|
try std.testing.expect(d.insert(key(0x08080808, 443)));
|
||||||
|
try std.testing.expect(!d.insert(key(0x08080808, 80)));
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), d.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "distinct keys are all retained across growth" {
|
||||||
|
var d = try Dedup.init(std.testing.allocator, 4);
|
||||||
|
defer d.deinit();
|
||||||
|
var i: u32 = 0;
|
||||||
|
while (i < 1000) : (i += 1) {
|
||||||
|
try std.testing.expect(d.insert(key(0x08080000 + i, 80)));
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, 1000), d.len);
|
||||||
|
i = 0;
|
||||||
|
while (i < 1000) : (i += 1) {
|
||||||
|
try std.testing.expect(!d.insert(key(0x08080000 + i, 80)));
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, 1000), d.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the all-ones sentinel cannot collide with a real key" {
|
||||||
|
const max_key = key(0xffffffff, 0xffff);
|
||||||
|
try std.testing.expect(max_key != EMPTY);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0x0000ffffffffffff), max_key);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// main.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const cli = @import("cli");
|
||||||
|
const smoke = @import("smoke");
|
||||||
|
const txcmd = @import("txcmd");
|
||||||
|
const scancmd = @import("scancmd");
|
||||||
|
|
||||||
|
pub fn main(init: std.process.Init) !void {
|
||||||
|
const io = init.io;
|
||||||
|
const arena = init.arena.allocator();
|
||||||
|
const args = try init.minimal.args.toSlice(arena);
|
||||||
|
|
||||||
|
const cmd: []const u8 = if (args.len > 1) args[1] else "";
|
||||||
|
|
||||||
|
if (std.mem.eql(u8, cmd, "--version") or std.mem.eql(u8, cmd, "-V")) {
|
||||||
|
return cli.printVersion(io);
|
||||||
|
}
|
||||||
|
if (std.mem.eql(u8, cmd, "smoke")) {
|
||||||
|
const ifname: []const u8 = if (args.len > 2) args[2] else "lo";
|
||||||
|
return smoke.run(io, ifname);
|
||||||
|
}
|
||||||
|
if (std.mem.eql(u8, cmd, "tx")) {
|
||||||
|
return txcmd.run(io, arena, args);
|
||||||
|
}
|
||||||
|
if (std.mem.eql(u8, cmd, "scan")) {
|
||||||
|
return scancmd.run(io, arena, args, init.environ_map);
|
||||||
|
}
|
||||||
|
return cli.printHelp(io, init.environ_map);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,198 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// ndp.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
const ETH_P_ALL: u16 = 0x0003;
|
||||||
|
const ethertype_ipv6: u16 = 0x86dd;
|
||||||
|
const ip6_version_tc_flow: u32 = 0x60000000;
|
||||||
|
const ip6_next_icmpv6: u8 = 58;
|
||||||
|
const ndp_hop_limit: u8 = 255;
|
||||||
|
|
||||||
|
const icmp6_ns: u8 = 135;
|
||||||
|
const icmp6_na: u8 = 136;
|
||||||
|
const opt_src_lladdr: u8 = 1;
|
||||||
|
|
||||||
|
const eth_len: usize = 14;
|
||||||
|
const ip6_len: usize = 40;
|
||||||
|
const ns_len: usize = 24;
|
||||||
|
const opt_len: usize = 8;
|
||||||
|
pub const solicit_frame_len: usize = eth_len + ip6_len + ns_len + opt_len;
|
||||||
|
|
||||||
|
const icmp6_off: usize = eth_len + ip6_len;
|
||||||
|
const na_target_off: usize = icmp6_off + 8;
|
||||||
|
|
||||||
|
const NS_PER_MS: u64 = 1_000_000;
|
||||||
|
const NS_PER_SEC: u64 = 1_000_000_000;
|
||||||
|
|
||||||
|
fn solicitedNodeMulticast(target: [16]u8) [16]u8 {
|
||||||
|
return [16]u8{ 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0xff, target[13], target[14], target[15] };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn multicastMac(target: [16]u8) [6]u8 {
|
||||||
|
return [6]u8{ 0x33, 0x33, 0xff, target[13], target[14], target[15] };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn buildSolicit(our_ip6: [16]u8, our_mac: [6]u8, target: [16]u8) [solicit_frame_len]u8 {
|
||||||
|
var frame = [_]u8{0} ** solicit_frame_len;
|
||||||
|
const mcast = solicitedNodeMulticast(target);
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = multicastMac(target),
|
||||||
|
.src = our_mac,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, ethertype_ipv6),
|
||||||
|
};
|
||||||
|
@memcpy(frame[0..eth_len], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
const ip6 = packet.Ipv6Hdr{
|
||||||
|
.version_tc_flow = std.mem.nativeToBig(u32, ip6_version_tc_flow),
|
||||||
|
.payload_len = std.mem.nativeToBig(u16, ns_len + opt_len),
|
||||||
|
.next_header = ip6_next_icmpv6,
|
||||||
|
.hop_limit = ndp_hop_limit,
|
||||||
|
.src = our_ip6,
|
||||||
|
.dst = mcast,
|
||||||
|
};
|
||||||
|
@memcpy(frame[eth_len .. eth_len + ip6_len], std.mem.asBytes(&ip6));
|
||||||
|
|
||||||
|
frame[icmp6_off] = icmp6_ns;
|
||||||
|
frame[icmp6_off + 1] = 0;
|
||||||
|
@memcpy(frame[icmp6_off + 8 .. icmp6_off + 24], &target);
|
||||||
|
frame[icmp6_off + 24] = opt_src_lladdr;
|
||||||
|
frame[icmp6_off + 25] = 1;
|
||||||
|
@memcpy(frame[icmp6_off + 26 .. icmp6_off + 32], &our_mac);
|
||||||
|
|
||||||
|
const ck = packet.pseudoChecksum6(our_ip6, mcast, ip6_next_icmpv6, frame[icmp6_off..]);
|
||||||
|
std.mem.writeInt(u16, frame[icmp6_off + 2 ..][0..2], ck, .big);
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn matchNa(frame: []const u8, target: [16]u8) ?[6]u8 {
|
||||||
|
if (frame.len < eth_len + ip6_len + 24) return null;
|
||||||
|
if (std.mem.readInt(u16, frame[12..14], .big) != ethertype_ipv6) return null;
|
||||||
|
if (frame[eth_len + 6] != ip6_next_icmpv6) return null;
|
||||||
|
if (frame[eth_len + 7] != ndp_hop_limit) return null;
|
||||||
|
if (frame[icmp6_off] != icmp6_na) return null;
|
||||||
|
if (frame[icmp6_off + 1] != 0) return null;
|
||||||
|
if (target[0] == 0xff) return null;
|
||||||
|
if (!std.mem.eql(u8, frame[na_target_off .. na_target_off + 16], &target)) return null;
|
||||||
|
return frame[6..12].*;
|
||||||
|
}
|
||||||
|
|
||||||
|
const OpenError = error{ NeedCapNetRaw, Failed };
|
||||||
|
|
||||||
|
fn openBound(ifname: []const u8) OpenError!struct { fd: i32, sll: linux.sockaddr.ll } {
|
||||||
|
const rc_sock = linux.socket(linux.AF.PACKET, linux.SOCK.RAW, std.mem.nativeToBig(u16, ETH_P_ALL));
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.Failed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.Failed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS) return error.Failed;
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, ETH_P_ALL);
|
||||||
|
sll.ifindex = ifr.ifru.ivalue;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS) return error.Failed;
|
||||||
|
return .{ .fd = fd, .sll = sll };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn monoMs() i64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(i64, @intCast(ts.sec)) * 1000 + @divTrunc(@as(i64, @intCast(ts.nsec)), NS_PER_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const ResolveError = error{ NeedCapNetRaw, SocketFailed, NoNeighbor };
|
||||||
|
|
||||||
|
pub fn resolve(ifname: []const u8, our_ip6: [16]u8, our_mac: [6]u8, target: [16]u8, timeout_ms: i64) ResolveError![6]u8 {
|
||||||
|
const s = openBound(ifname) catch |e| return switch (e) {
|
||||||
|
error.NeedCapNetRaw => error.NeedCapNetRaw,
|
||||||
|
error.Failed => error.SocketFailed,
|
||||||
|
};
|
||||||
|
defer _ = linux.close(s.fd);
|
||||||
|
var sll = s.sll;
|
||||||
|
|
||||||
|
const frame = buildSolicit(our_ip6, our_mac, target);
|
||||||
|
var send_round: usize = 0;
|
||||||
|
while (send_round < 3) : (send_round += 1) {
|
||||||
|
_ = linux.sendto(s.fd, &frame, frame.len, 0, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll));
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf: [2048]u8 = undefined;
|
||||||
|
const deadline = monoMs() + timeout_ms;
|
||||||
|
while (monoMs() < deadline) {
|
||||||
|
var pfd = [_]linux.pollfd{.{ .fd = s.fd, .events = linux.POLL.IN, .revents = 0 }};
|
||||||
|
const pr = linux.poll(&pfd, 1, 50);
|
||||||
|
switch (linux.errno(pr)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return error.NoNeighbor,
|
||||||
|
}
|
||||||
|
if (pr == 0) continue;
|
||||||
|
const rc = linux.recvfrom(s.fd, &buf, buf.len, 0, null, null);
|
||||||
|
switch (linux.errno(rc)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR, .AGAIN => continue,
|
||||||
|
else => return error.NoNeighbor,
|
||||||
|
}
|
||||||
|
const n: usize = @intCast(rc);
|
||||||
|
if (matchNa(buf[0..n], target)) |mac| return mac;
|
||||||
|
}
|
||||||
|
return error.NoNeighbor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- tests ----
|
||||||
|
|
||||||
|
const k_our_ip6 = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 };
|
||||||
|
const k_our_mac = [6]u8{ 0x02, 0xaa, 0xbb, 0xcc, 0xdd, 0x01 };
|
||||||
|
const k_target = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x11, 0x22, 0x33 };
|
||||||
|
|
||||||
|
test "solicited-node multicast + MAC derive from the target's low 24 bits" {
|
||||||
|
try std.testing.expectEqual([16]u8{ 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0xff, 0x11, 0x22, 0x33 }, solicitedNodeMulticast(k_target));
|
||||||
|
try std.testing.expectEqual([6]u8{ 0x33, 0x33, 0xff, 0x11, 0x22, 0x33 }, multicastMac(k_target));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "buildSolicit produces a hop-limit-255 NS with a self-verifying ICMPv6 checksum" {
|
||||||
|
const f = buildSolicit(k_our_ip6, k_our_mac, k_target);
|
||||||
|
try std.testing.expectEqual(@as(usize, 86), f.len);
|
||||||
|
try std.testing.expectEqual(@as(u16, ethertype_ipv6), std.mem.readInt(u16, f[12..14], .big));
|
||||||
|
try std.testing.expectEqualSlices(u8, &multicastMac(k_target), f[0..6]);
|
||||||
|
try std.testing.expectEqual(ndp_hop_limit, f[eth_len + 7]);
|
||||||
|
try std.testing.expectEqual(icmp6_ns, f[icmp6_off]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &k_target, f[icmp6_off + 8 .. icmp6_off + 24]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &k_our_mac, f[icmp6_off + 26 .. icmp6_off + 32]);
|
||||||
|
const mcast = solicitedNodeMulticast(k_target);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.pseudoChecksum6(k_our_ip6, mcast, ip6_next_icmpv6, f[icmp6_off..]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "matchNa returns the neighbor MAC for a matching advertisement and rejects mismatches" {
|
||||||
|
const neighbor_mac = [6]u8{ 0x02, 0x11, 0x22, 0x33, 0x44, 0x55 };
|
||||||
|
var na = [_]u8{0} ** 78;
|
||||||
|
@memcpy(na[6..12], &neighbor_mac);
|
||||||
|
std.mem.writeInt(u16, na[12..14], ethertype_ipv6, .big);
|
||||||
|
na[eth_len + 6] = ip6_next_icmpv6;
|
||||||
|
na[eth_len + 7] = ndp_hop_limit;
|
||||||
|
na[icmp6_off] = icmp6_na;
|
||||||
|
@memcpy(na[na_target_off .. na_target_off + 16], &k_target);
|
||||||
|
try std.testing.expectEqual(neighbor_mac, matchNa(&na, k_target).?);
|
||||||
|
|
||||||
|
var low_hop = na;
|
||||||
|
low_hop[eth_len + 7] = 64;
|
||||||
|
try std.testing.expect(matchNa(&low_hop, k_target) == null);
|
||||||
|
|
||||||
|
var wrong = k_target;
|
||||||
|
wrong[15] = 0x99;
|
||||||
|
try std.testing.expect(matchNa(&na, wrong) == null);
|
||||||
|
|
||||||
|
na[icmp6_off] = icmp6_ns;
|
||||||
|
try std.testing.expect(matchNa(&na, k_target) == null);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,305 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// netutil.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
pub fn getFlag(args: []const []const u8, name: []const u8) ?[]const u8 {
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + 1 < args.len) : (i += 1) {
|
||||||
|
if (std.mem.eql(u8, args[i], name)) return args[i + 1];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn hasFlag(args: []const []const u8, name: []const u8) bool {
|
||||||
|
for (args) |a| {
|
||||||
|
if (std.mem.eql(u8, a, name)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parseIpv4(text: []const u8) !u32 {
|
||||||
|
var addr: u32 = 0;
|
||||||
|
var octets: usize = 0;
|
||||||
|
var it = std.mem.splitScalar(u8, text, '.');
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (octets == 4) return error.InvalidIpv4;
|
||||||
|
const octet = std.fmt.parseInt(u8, part, 10) catch return error.InvalidIpv4;
|
||||||
|
addr = (addr << 8) | octet;
|
||||||
|
octets += 1;
|
||||||
|
}
|
||||||
|
if (octets != 4) return error.InvalidIpv4;
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseV6Groups(text: []const u8, out: *[8]u16) !usize {
|
||||||
|
if (text.len == 0) return 0;
|
||||||
|
var n: usize = 0;
|
||||||
|
var it = std.mem.splitScalar(u8, text, ':');
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (n >= 8) return error.InvalidIpv6;
|
||||||
|
if (part.len == 0 or part.len > 4) return error.InvalidIpv6;
|
||||||
|
for (part) |c| if (!std.ascii.isHex(c)) return error.InvalidIpv6;
|
||||||
|
out[n] = std.fmt.parseInt(u16, part, 16) catch return error.InvalidIpv6;
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parseIpv6(text: []const u8) ![16]u8 {
|
||||||
|
if (text.len == 0 or text.len > 45) return error.InvalidIpv6;
|
||||||
|
var out = [_]u8{0} ** 16;
|
||||||
|
|
||||||
|
if (std.mem.indexOf(u8, text, "::")) |pos| {
|
||||||
|
if (std.mem.indexOf(u8, text[pos + 2 ..], "::") != null) return error.InvalidIpv6;
|
||||||
|
var left: [8]u16 = undefined;
|
||||||
|
var right: [8]u16 = undefined;
|
||||||
|
const ln = try parseV6Groups(text[0..pos], &left);
|
||||||
|
const rn = try parseV6Groups(text[pos + 2 ..], &right);
|
||||||
|
if (ln + rn > 7) return error.InvalidIpv6;
|
||||||
|
for (0..ln) |i| std.mem.writeInt(u16, out[i * 2 ..][0..2], left[i], .big);
|
||||||
|
for (0..rn) |i| std.mem.writeInt(u16, out[(8 - rn + i) * 2 ..][0..2], right[i], .big);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
var groups: [8]u16 = undefined;
|
||||||
|
const n = try parseV6Groups(text, &groups);
|
||||||
|
if (n != 8) return error.InvalidIpv6;
|
||||||
|
for (0..8) |i| std.mem.writeInt(u16, out[i * 2 ..][0..2], groups[i], .big);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parseMac(text: []const u8) ![6]u8 {
|
||||||
|
var mac: [6]u8 = undefined;
|
||||||
|
var octets: usize = 0;
|
||||||
|
var it = std.mem.splitScalar(u8, text, ':');
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (octets == 6) return error.InvalidMac;
|
||||||
|
mac[octets] = std.fmt.parseInt(u8, part, 16) catch return error.InvalidMac;
|
||||||
|
octets += 1;
|
||||||
|
}
|
||||||
|
if (octets != 6) return error.InvalidMac;
|
||||||
|
return mac;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parsePorts(allocator: std.mem.Allocator, text: []const u8) ![]u16 {
|
||||||
|
var list: std.ArrayList(u16) = .empty;
|
||||||
|
errdefer list.deinit(allocator);
|
||||||
|
var it = std.mem.splitScalar(u8, text, ',');
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (part.len == 0) continue;
|
||||||
|
const port = std.fmt.parseInt(u16, part, 10) catch return error.InvalidPort;
|
||||||
|
try list.append(allocator, port);
|
||||||
|
}
|
||||||
|
if (list.items.len == 0) return error.InvalidPort;
|
||||||
|
return list.toOwnedSlice(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ifaceQuery(ifname: []const u8, request: u32) !linux.sockaddr {
|
||||||
|
const rc_sock = linux.socket(linux.AF.INET, linux.SOCK.DGRAM, 0);
|
||||||
|
if (linux.errno(rc_sock) != .SUCCESS) return error.ResolveSocketFailed;
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
defer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.IfNameTooLong;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, request, @intFromPtr(&ifr))) != .SUCCESS) return error.IfQueryFailed;
|
||||||
|
return ifr.ifru.addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolveSrcIp(ifname: []const u8) !u32 {
|
||||||
|
const sa = try ifaceQuery(ifname, linux.SIOCGIFADDR);
|
||||||
|
return std.mem.readInt(u32, sa.data[2..6], .big);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolveSrcMac(ifname: []const u8) ![6]u8 {
|
||||||
|
const sa = try ifaceQuery(ifname, linux.SIOCGIFHWADDR);
|
||||||
|
return sa.data[0..6].*;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseIfInet6(contents: []const u8, ifname: []const u8) ?[16]u8 {
|
||||||
|
var fallback: ?[16]u8 = null;
|
||||||
|
var lines = std.mem.splitScalar(u8, contents, '\n');
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
var it = std.mem.tokenizeAny(u8, line, " \t");
|
||||||
|
const addr_hex = it.next() orelse continue;
|
||||||
|
_ = it.next() orelse continue;
|
||||||
|
_ = it.next() orelse continue;
|
||||||
|
const scope = it.next() orelse continue;
|
||||||
|
_ = it.next() orelse continue;
|
||||||
|
const dev = it.next() orelse continue;
|
||||||
|
if (!std.mem.eql(u8, dev, ifname)) continue;
|
||||||
|
if (addr_hex.len != 32) continue;
|
||||||
|
var addr: [16]u8 = undefined;
|
||||||
|
_ = std.fmt.hexToBytes(&addr, addr_hex) catch continue;
|
||||||
|
if (std.mem.eql(u8, scope, "00")) return addr;
|
||||||
|
if (fallback == null) fallback = addr;
|
||||||
|
}
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolveSrcIp6(ifname: []const u8) ![16]u8 {
|
||||||
|
const rc = linux.openat(linux.AT.FDCWD, "/proc/net/if_inet6", .{}, 0);
|
||||||
|
if (linux.errno(rc) != .SUCCESS) return error.NoV6Address;
|
||||||
|
const fd: i32 = @intCast(rc);
|
||||||
|
defer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var buf: [16384]u8 = undefined;
|
||||||
|
var total: usize = 0;
|
||||||
|
while (total < buf.len) {
|
||||||
|
const n_rc = linux.read(fd, buf[total..].ptr, buf.len - total);
|
||||||
|
switch (linux.errno(n_rc)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return error.NoV6Address,
|
||||||
|
}
|
||||||
|
const n: usize = @intCast(n_rc);
|
||||||
|
if (n == 0) break;
|
||||||
|
total += n;
|
||||||
|
}
|
||||||
|
return parseIfInet6(buf[0..total], ifname) orelse error.NoV6Address;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseIpv6Route(contents: []const u8, ifname: []const u8) ?[16]u8 {
|
||||||
|
var lines = std.mem.splitScalar(u8, contents, '\n');
|
||||||
|
while (lines.next()) |line| {
|
||||||
|
var it = std.mem.tokenizeAny(u8, line, " \t");
|
||||||
|
const dest = it.next() orelse continue;
|
||||||
|
const dest_plen = it.next() orelse continue;
|
||||||
|
_ = it.next() orelse continue;
|
||||||
|
_ = it.next() orelse continue;
|
||||||
|
const nexthop = it.next() orelse continue;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < 4) : (i += 1) _ = it.next() orelse break;
|
||||||
|
const dev = it.next() orelse continue;
|
||||||
|
if (!std.mem.eql(u8, dev, ifname)) continue;
|
||||||
|
if (!std.mem.eql(u8, dest_plen, "00")) continue;
|
||||||
|
if (dest.len != 32 or nexthop.len != 32) continue;
|
||||||
|
var addr: [16]u8 = undefined;
|
||||||
|
_ = std.fmt.hexToBytes(&addr, nexthop) catch continue;
|
||||||
|
if (std.mem.allEqual(u8, &addr, 0)) continue;
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn defaultGateway6(ifname: []const u8) ?[16]u8 {
|
||||||
|
const rc = linux.openat(linux.AT.FDCWD, "/proc/net/ipv6_route", .{}, 0);
|
||||||
|
if (linux.errno(rc) != .SUCCESS) return null;
|
||||||
|
const fd: i32 = @intCast(rc);
|
||||||
|
defer _ = linux.close(fd);
|
||||||
|
var buf: [65536]u8 = undefined;
|
||||||
|
var total: usize = 0;
|
||||||
|
while (total < buf.len) {
|
||||||
|
const n_rc = linux.read(fd, buf[total..].ptr, buf.len - total);
|
||||||
|
switch (linux.errno(n_rc)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
const n: usize = @intCast(n_rc);
|
||||||
|
if (n == 0) break;
|
||||||
|
total += n;
|
||||||
|
}
|
||||||
|
return parseIpv6Route(buf[0..total], ifname);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const RealClock = struct {
|
||||||
|
pub fn now(_: *RealClock) u64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(u64, @intCast(ts.sec)) * 1_000_000_000 + @as(u64, @intCast(ts.nsec));
|
||||||
|
}
|
||||||
|
pub fn sleepNs(_: *RealClock, ns: u64) void {
|
||||||
|
const ts = linux.timespec{
|
||||||
|
.sec = @intCast(ns / 1_000_000_000),
|
||||||
|
.nsec = @intCast(ns % 1_000_000_000),
|
||||||
|
};
|
||||||
|
_ = linux.nanosleep(&ts, null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "parseIpv4 round-trips dotted quads" {
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x7f000001), try parseIpv4("127.0.0.1"));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x08080808), try parseIpv4("8.8.8.8"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv4, parseIpv4("1.2.3"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv4, parseIpv4("256.0.0.1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseIpv6 handles compression, full form, and edge positions" {
|
||||||
|
try std.testing.expectEqual([16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, try parseIpv6("::1"));
|
||||||
|
try std.testing.expectEqual([16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, try parseIpv6("::"));
|
||||||
|
try std.testing.expectEqual([16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, try parseIpv6("fe80::"));
|
||||||
|
const a = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||||
|
try std.testing.expectEqual(a, try parseIpv6("2001:db8::1"));
|
||||||
|
try std.testing.expectEqual(a, try parseIpv6("2001:db8:0:0:0:0:0:1"));
|
||||||
|
try std.testing.expectEqual(a, try parseIpv6("2001:0db8:0000:0000:0000:0000:0000:0001"));
|
||||||
|
try std.testing.expectEqual([16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, try parseIpv6("2001:db8:1::1"));
|
||||||
|
try std.testing.expectEqual([16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc0, 0xa8, 0, 1 }, try parseIpv6("::ffff:c0a8:1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseIpv6 rejects malformed literals" {
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6(""));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("1:2:3"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6(":1"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("1::2::3"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("gggg::"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("1:2:3:4:5:6:7:8:9"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("12345::"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("2001:+db8::1"));
|
||||||
|
try std.testing.expectError(error.InvalidIpv6, parseIpv6("2001:d_b8::1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseIfInet6 prefers a global-scope address and falls back to link-local" {
|
||||||
|
const contents =
|
||||||
|
"fe80000000000000e81e99fffeae865d 1b245 40 20 80 veth0\n" ++
|
||||||
|
"20010db8000000000000000000000001 1b245 40 00 80 veth0\n" ++
|
||||||
|
"00000000000000000000000000000001 00001 80 10 80 lo\n";
|
||||||
|
const g = parseIfInet6(contents, "veth0").?;
|
||||||
|
try std.testing.expectEqual([16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, g);
|
||||||
|
|
||||||
|
const only_ll = "fe80000000000000e81e99fffeae865d 1b245 40 20 80 eth9\n";
|
||||||
|
const ll = parseIfInet6(only_ll, "eth9").?;
|
||||||
|
try std.testing.expectEqual(@as(u8, 0xfe), ll[0]);
|
||||||
|
try std.testing.expectEqual(@as(u8, 0x80), ll[1]);
|
||||||
|
|
||||||
|
try std.testing.expect(parseIfInet6(contents, "eth0") == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseIpv6Route returns the default-route nexthop for the matching iface" {
|
||||||
|
const contents =
|
||||||
|
"20010db8000000000000000000000000 40 00000000000000000000000000000000 00 00000000000000000000000000000000 00000100 00000000 00000000 00000001 veth0\n" ++
|
||||||
|
"00000000000000000000000000000000 00 00000000000000000000000000000000 00 fe800000000000000000000000000001 00000400 00000000 00000000 00000003 veth0\n";
|
||||||
|
const gw = parseIpv6Route(contents, "veth0").?;
|
||||||
|
try std.testing.expectEqual([16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, gw);
|
||||||
|
try std.testing.expect(parseIpv6Route(contents, "eth9") == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseMac parses colon-separated hex" {
|
||||||
|
try std.testing.expectEqual([6]u8{ 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff }, try parseMac("aa:bb:cc:dd:ee:ff"));
|
||||||
|
try std.testing.expectEqual([_]u8{0} ** 6, try parseMac("00:00:00:00:00:00"));
|
||||||
|
try std.testing.expectError(error.InvalidMac, parseMac("aa:bb:cc"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parsePorts parses a comma list and rejects empty" {
|
||||||
|
const ports = try parsePorts(std.testing.allocator, "80,443,22");
|
||||||
|
defer std.testing.allocator.free(ports);
|
||||||
|
try std.testing.expectEqualSlices(u16, &.{ 80, 443, 22 }, ports);
|
||||||
|
try std.testing.expectError(error.InvalidPort, parsePorts(std.testing.allocator, ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "getFlag finds values and tolerates missing" {
|
||||||
|
const args = [_][]const u8{ "tx", "--iface", "eth0", "--rate", "5000" };
|
||||||
|
try std.testing.expectEqualStrings("eth0", getFlag(&args, "--iface").?);
|
||||||
|
try std.testing.expectEqualStrings("5000", getFlag(&args, "--rate").?);
|
||||||
|
try std.testing.expect(getFlag(&args, "--target") == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "hasFlag detects a valueless boolean flag in any position" {
|
||||||
|
const args = [_][]const u8{ "scan", "--target", "10.0.0.0/24", "--json" };
|
||||||
|
try std.testing.expect(hasFlag(&args, "--json"));
|
||||||
|
try std.testing.expect(hasFlag(&args, "scan"));
|
||||||
|
try std.testing.expect(!hasFlag(&args, "--nope"));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,148 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// numtheory.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn mulMod(a: u64, b: u64, m: u64) u64 {
|
||||||
|
return @intCast((@as(u128, a) * @as(u128, b)) % m);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn modExp(base: u64, exp: u64, modulus: u64) u64 {
|
||||||
|
if (modulus == 1) return 0;
|
||||||
|
var result: u64 = 1;
|
||||||
|
var b: u64 = base % modulus;
|
||||||
|
var e: u64 = exp;
|
||||||
|
while (e > 0) {
|
||||||
|
if (e & 1 == 1) result = mulMod(result, b, modulus);
|
||||||
|
b = mulMod(b, b, modulus);
|
||||||
|
e >>= 1;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isPrime(n: u64) bool {
|
||||||
|
if (n < 2) return false;
|
||||||
|
const small = [_]u64{ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
|
||||||
|
for (small) |p| {
|
||||||
|
if (n == p) return true;
|
||||||
|
if (n % p == 0) return false;
|
||||||
|
}
|
||||||
|
var d: u64 = n - 1;
|
||||||
|
var r: u32 = 0;
|
||||||
|
while (d & 1 == 0) : (d >>= 1) r += 1;
|
||||||
|
for (small) |a| {
|
||||||
|
var x = modExp(a, d, n);
|
||||||
|
if (x == 1 or x == n - 1) continue;
|
||||||
|
var i: u32 = 1;
|
||||||
|
var composite = true;
|
||||||
|
while (i < r) : (i += 1) {
|
||||||
|
x = mulMod(x, x, n);
|
||||||
|
if (x == n - 1) {
|
||||||
|
composite = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (composite) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn smallestPrimeAbove(n: u64) u64 {
|
||||||
|
var candidate = n + 1;
|
||||||
|
if (candidate <= 2) return 2;
|
||||||
|
if (candidate & 1 == 0) candidate += 1;
|
||||||
|
while (!isPrime(candidate)) : (candidate += 2) {}
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn distinctPrimeFactors(value: u64, buf: []u64) []u64 {
|
||||||
|
var n = value;
|
||||||
|
var count: usize = 0;
|
||||||
|
var f: u64 = 2;
|
||||||
|
while (f * f <= n) {
|
||||||
|
if (n % f == 0) {
|
||||||
|
buf[count] = f;
|
||||||
|
count += 1;
|
||||||
|
while (n % f == 0) n /= f;
|
||||||
|
}
|
||||||
|
f += if (f == 2) 1 else 2;
|
||||||
|
}
|
||||||
|
if (n > 1) {
|
||||||
|
buf[count] = n;
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
return buf[0..count];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isPrimitiveRoot(candidate: u64, prime: u64, prime_factors: []const u64) bool {
|
||||||
|
for (prime_factors) |q| {
|
||||||
|
if (modExp(candidate, (prime - 1) / q, prime) == 1) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn findPrimitiveRoot(prime: u64, rand: std.Random) u64 {
|
||||||
|
if (prime == 2) return 1;
|
||||||
|
var buf: [64]u64 = undefined;
|
||||||
|
const factors = distinctPrimeFactors(prime - 1, &buf);
|
||||||
|
while (true) {
|
||||||
|
const candidate = rand.intRangeAtMost(u64, 2, prime - 1);
|
||||||
|
if (isPrimitiveRoot(candidate, prime, factors)) return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "modExp known values" {
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), modExp(3, 4, 5));
|
||||||
|
try std.testing.expectEqual(@as(u64, 24), modExp(2, 10, 1000));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), modExp(10, 3, 1000));
|
||||||
|
try std.testing.expectEqual(@as(u64, 445), modExp(4, 13, 497));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "isPrime classifies small and large values" {
|
||||||
|
const primes = [_]u64{ 2, 3, 5, 7, 11, 13, 65537, 1009, 4294967311, 281474976710677 };
|
||||||
|
for (primes) |p| try std.testing.expect(isPrime(p));
|
||||||
|
const composites = [_]u64{ 0, 1, 4, 9, 15, 561, 1105, 4294967296, 281474976710676 };
|
||||||
|
for (composites) |c| try std.testing.expect(!isPrime(c));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "smallestPrimeAbove" {
|
||||||
|
try std.testing.expectEqual(@as(u64, 257), smallestPrimeAbove(256));
|
||||||
|
try std.testing.expectEqual(@as(u64, 65537), smallestPrimeAbove(65536));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1009), smallestPrimeAbove(1000));
|
||||||
|
try std.testing.expectEqual(@as(u64, 4294967311), smallestPrimeAbove(4294967296));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "distinctPrimeFactors" {
|
||||||
|
var buf: [16]u64 = undefined;
|
||||||
|
try std.testing.expectEqualSlices(u64, &.{2}, distinctPrimeFactors(256, &buf));
|
||||||
|
try std.testing.expectEqualSlices(u64, &.{ 2, 3 }, distinctPrimeFactors(12, &buf));
|
||||||
|
try std.testing.expectEqualSlices(u64, &.{ 2, 5 }, distinctPrimeFactors(100, &buf));
|
||||||
|
try std.testing.expectEqualSlices(u64, &.{ 2, 3, 5 }, distinctPrimeFactors(30, &buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "isPrimitiveRoot for p=7 (roots are 3 and 5)" {
|
||||||
|
var buf: [16]u64 = undefined;
|
||||||
|
const factors = distinctPrimeFactors(7 - 1, &buf);
|
||||||
|
try std.testing.expect(isPrimitiveRoot(3, 7, factors));
|
||||||
|
try std.testing.expect(isPrimitiveRoot(5, 7, factors));
|
||||||
|
try std.testing.expect(!isPrimitiveRoot(2, 7, factors));
|
||||||
|
try std.testing.expect(!isPrimitiveRoot(4, 7, factors));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "findPrimitiveRoot returns a generator that walks the whole group" {
|
||||||
|
var prng = std.Random.DefaultPrng.init(0xA11CE_2026);
|
||||||
|
const rand = prng.random();
|
||||||
|
const primes = [_]u64{ 7, 257, 65537, 1009 };
|
||||||
|
for (primes) |p| {
|
||||||
|
const g = findPrimitiveRoot(p, rand);
|
||||||
|
var seen = [_]bool{false} ** 65537;
|
||||||
|
var cur: u64 = 1;
|
||||||
|
var k: u64 = 0;
|
||||||
|
while (k < p - 1) : (k += 1) {
|
||||||
|
cur = mulMod(cur, g, p);
|
||||||
|
try std.testing.expect(!seen[cur]);
|
||||||
|
seen[cur] = true;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,858 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// output.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const classify = @import("classify");
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
pub const State = classify.State;
|
||||||
|
pub const Result = classify.Result;
|
||||||
|
|
||||||
|
const esc = "\x1b";
|
||||||
|
const sgr_reset = "\x1b[0m";
|
||||||
|
const clear_line = "\x1b[2K";
|
||||||
|
const block_full = "\u{2588}";
|
||||||
|
const block_light = "\u{2591}";
|
||||||
|
const gutter_bar = "\u{258e}";
|
||||||
|
const box_h = "\u{2500}";
|
||||||
|
|
||||||
|
const cache_line = std.atomic.cache_line;
|
||||||
|
|
||||||
|
pub const Counter = std.atomic.Value(u64);
|
||||||
|
|
||||||
|
pub const Padded = struct {
|
||||||
|
v: Counter align(cache_line) = .{ .raw = 0 },
|
||||||
|
_pad: [cache_line - @sizeOf(Counter)]u8 = undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Stats = struct {
|
||||||
|
sent: Padded = .{},
|
||||||
|
found: Padded = .{},
|
||||||
|
open: Padded = .{},
|
||||||
|
closed: Padded = .{},
|
||||||
|
filtered: Padded = .{},
|
||||||
|
unfiltered: Padded = .{},
|
||||||
|
banners: Padded = .{},
|
||||||
|
|
||||||
|
pub fn record(self: *Stats, st: State) void {
|
||||||
|
_ = self.found.v.fetchAdd(1, .monotonic);
|
||||||
|
switch (st) {
|
||||||
|
.open => _ = self.open.v.fetchAdd(1, .monotonic),
|
||||||
|
.closed => _ = self.closed.v.fetchAdd(1, .monotonic),
|
||||||
|
.filtered => _ = self.filtered.v.fetchAdd(1, .monotonic),
|
||||||
|
.unfiltered => _ = self.unfiltered.v.fetchAdd(1, .monotonic),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Rgb = struct { r: u8, g: u8, b: u8 };
|
||||||
|
|
||||||
|
const violet_light = Rgb{ .r = 167, .g = 139, .b = 250 };
|
||||||
|
const violet_mid = Rgb{ .r = 139, .g = 92, .b = 246 };
|
||||||
|
const violet_deep = Rgb{ .r = 109, .g = 74, .b = 255 };
|
||||||
|
const neon_green = Rgb{ .r = 74, .g = 222, .b = 128 };
|
||||||
|
const chrome_gray = Rgb{ .r = 120, .g = 120, .b = 140 };
|
||||||
|
const bright_white = Rgb{ .r = 230, .g = 230, .b = 240 };
|
||||||
|
const soft_amber = Rgb{ .r = 217, .g = 164, .b = 74 };
|
||||||
|
|
||||||
|
pub const ColorLevel = enum { none, ansi256, truecolor };
|
||||||
|
pub const ColorChoice = enum { auto, always, never };
|
||||||
|
|
||||||
|
pub fn parseColorChoice(text: ?[]const u8) ColorChoice {
|
||||||
|
const t = text orelse return .auto;
|
||||||
|
if (std.mem.eql(u8, t, "always")) return .always;
|
||||||
|
if (std.mem.eql(u8, t, "never")) return .never;
|
||||||
|
return .auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn resolveLevel(choice: ColorChoice, colored: bool, truecolor: bool) ColorLevel {
|
||||||
|
return switch (choice) {
|
||||||
|
.never => .none,
|
||||||
|
.always => if (truecolor) .truecolor else .ansi256,
|
||||||
|
.auto => if (!colored) .none else if (truecolor) .truecolor else .ansi256,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn detectLevel(
|
||||||
|
io: std.Io,
|
||||||
|
file: std.Io.File,
|
||||||
|
choice: ColorChoice,
|
||||||
|
no_color: bool,
|
||||||
|
clicolor_force: bool,
|
||||||
|
truecolor: bool,
|
||||||
|
) ColorLevel {
|
||||||
|
switch (choice) {
|
||||||
|
.never => return .none,
|
||||||
|
.always => return resolveLevel(.always, true, truecolor),
|
||||||
|
.auto => {
|
||||||
|
const mode = std.Io.Terminal.Mode.detect(io, file, no_color, clicolor_force) catch return .none;
|
||||||
|
const colored = switch (mode) {
|
||||||
|
.no_color => false,
|
||||||
|
else => true,
|
||||||
|
};
|
||||||
|
return resolveLevel(.auto, colored, truecolor);
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn envLevel(io: std.Io, file: std.Io.File, env: *std.process.Environ.Map, choice: ColorChoice) ColorLevel {
|
||||||
|
const no_color = if (env.get("NO_COLOR")) |v| v.len > 0 else false;
|
||||||
|
const clicolor_force = if (env.get("CLICOLOR_FORCE")) |v| (v.len > 0 and !std.mem.eql(u8, v, "0")) else false;
|
||||||
|
const colorterm = env.get("COLORTERM") orelse "";
|
||||||
|
const truecolor = std.mem.eql(u8, colorterm, "truecolor") or std.mem.eql(u8, colorterm, "24bit");
|
||||||
|
return detectLevel(io, file, choice, no_color, clicolor_force, truecolor);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn bannerWordmark(out: *std.Io.Writer, level: ColorLevel, art: []const u8) !void {
|
||||||
|
if (level == .none) {
|
||||||
|
try out.print("{s}\n", .{art});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const span_lines: f32 = 4.0;
|
||||||
|
var it = std.mem.splitScalar(u8, art, '\n');
|
||||||
|
var i: usize = 0;
|
||||||
|
while (it.next()) |line| : (i += 1) {
|
||||||
|
const t = @min(@as(f32, @floatFromInt(i)), span_lines) / span_lines;
|
||||||
|
try setFg(out, level, violetAt(t));
|
||||||
|
try out.print("{s}\n", .{line});
|
||||||
|
}
|
||||||
|
try resetFg(out, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to256(c: Rgb) u8 {
|
||||||
|
const r6: u16 = (@as(u16, c.r) * 5 + 127) / 255;
|
||||||
|
const g6: u16 = (@as(u16, c.g) * 5 + 127) / 255;
|
||||||
|
const b6: u16 = (@as(u16, c.b) * 5 + 127) / 255;
|
||||||
|
return @intCast(16 + 36 * r6 + 6 * g6 + b6);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setFg(out: *std.Io.Writer, level: ColorLevel, c: Rgb) !void {
|
||||||
|
switch (level) {
|
||||||
|
.none => {},
|
||||||
|
.truecolor => try out.print("\x1b[38;2;{d};{d};{d}m", .{ c.r, c.g, c.b }),
|
||||||
|
.ansi256 => try out.print("\x1b[38;5;{d}m", .{to256(c)}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resetFg(out: *std.Io.Writer, level: ColorLevel) !void {
|
||||||
|
if (level != .none) try out.writeAll(sgr_reset);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn span(out: *std.Io.Writer, level: ColorLevel, c: Rgb, text: []const u8) !void {
|
||||||
|
try setFg(out, level, c);
|
||||||
|
try out.writeAll(text);
|
||||||
|
try resetFg(out, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lerpByte(a: u8, b: u8, t: f32) u8 {
|
||||||
|
const af: f32 = @floatFromInt(a);
|
||||||
|
const bf: f32 = @floatFromInt(b);
|
||||||
|
return @intFromFloat(std.math.clamp(af + (bf - af) * t, 0.0, 255.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lerp(a: Rgb, b: Rgb, t: f32) Rgb {
|
||||||
|
return .{ .r = lerpByte(a.r, b.r, t), .g = lerpByte(a.g, b.g, t), .b = lerpByte(a.b, b.b, t) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn violetAt(t: f32) Rgb {
|
||||||
|
const tc = std.math.clamp(t, 0.0, 1.0);
|
||||||
|
if (tc <= 0.5) return lerp(violet_light, violet_mid, tc * 2.0);
|
||||||
|
return lerp(violet_mid, violet_deep, (tc - 0.5) * 2.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gradientText(out: *std.Io.Writer, level: ColorLevel, text: []const u8) !void {
|
||||||
|
if (level == .none) {
|
||||||
|
try out.writeAll(text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const n = text.len;
|
||||||
|
for (text, 0..) |ch, i| {
|
||||||
|
const t: f32 = if (n <= 1) 0.0 else @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(n - 1));
|
||||||
|
try setFg(out, level, violetAt(t));
|
||||||
|
try out.writeByte(ch);
|
||||||
|
}
|
||||||
|
try resetFg(out, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
const bar_width: usize = 22;
|
||||||
|
|
||||||
|
fn progressBar(out: *std.Io.Writer, level: ColorLevel, frac: f64) !void {
|
||||||
|
const clamped = std.math.clamp(frac, 0.0, 1.0);
|
||||||
|
const filled: usize = @intFromFloat(clamped * @as(f64, @floatFromInt(bar_width)));
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < bar_width) : (i += 1) {
|
||||||
|
if (i < filled) {
|
||||||
|
const t: f32 = if (bar_width <= 1) 0.0 else @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(bar_width - 1));
|
||||||
|
try setFg(out, level, violetAt(t));
|
||||||
|
try out.writeAll(block_full);
|
||||||
|
} else {
|
||||||
|
try setFg(out, level, chrome_gray);
|
||||||
|
try out.writeAll(block_light);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try resetFg(out, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writeThousands(out: *std.Io.Writer, n: u64) !void {
|
||||||
|
var buf: [24]u8 = undefined;
|
||||||
|
const s = std.fmt.bufPrint(&buf, "{d}", .{n}) catch return;
|
||||||
|
for (s, 0..) |ch, i| {
|
||||||
|
if (i != 0 and (s.len - i) % 3 == 0) try out.writeByte(',');
|
||||||
|
try out.writeByte(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writeClock(out: *std.Io.Writer, secs: u64) !void {
|
||||||
|
try out.print("{d:0>2}:{d:0>2}:{d:0>2}", .{ secs / 3600, (secs % 3600) / 60, secs % 60 });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writeIp(out: *std.Io.Writer, ip: u32) !void {
|
||||||
|
try out.print("{d}.{d}.{d}.{d}", .{ (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const max_addr_str: usize = 45;
|
||||||
|
|
||||||
|
fn writeIp6(out: *std.Io.Writer, b: [16]u8) !void {
|
||||||
|
var groups: [8]u16 = undefined;
|
||||||
|
for (0..8) |i| groups[i] = (@as(u16, b[i * 2]) << 8) | b[i * 2 + 1];
|
||||||
|
|
||||||
|
var best_start: usize = 8;
|
||||||
|
var best_len: usize = 1;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < 8) {
|
||||||
|
if (groups[i] != 0) {
|
||||||
|
i += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var j = i + 1;
|
||||||
|
while (j < 8 and groups[j] == 0) j += 1;
|
||||||
|
if (j - i > best_len) {
|
||||||
|
best_len = j - i;
|
||||||
|
best_start = i;
|
||||||
|
}
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
const compress = best_start < 8;
|
||||||
|
|
||||||
|
var k: usize = 0;
|
||||||
|
var first = true;
|
||||||
|
while (k < 8) {
|
||||||
|
if (compress and k == best_start) {
|
||||||
|
try out.writeAll("::");
|
||||||
|
k += best_len;
|
||||||
|
first = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!first) try out.writeByte(':');
|
||||||
|
try out.print("{x}", .{groups[k]});
|
||||||
|
first = false;
|
||||||
|
k += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writeAddr(out: *std.Io.Writer, addr: packet.Addr) !void {
|
||||||
|
switch (addr) {
|
||||||
|
.v4 => |ip| try writeIp(out, ip),
|
||||||
|
.v6 => |b| try writeIp6(out, b),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn addrWidth(addr: packet.Addr) usize {
|
||||||
|
var buf: [max_addr_str]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
writeAddr(&w, addr) catch return max_addr_str;
|
||||||
|
return w.end;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stateName(st: State) []const u8 {
|
||||||
|
return switch (st) {
|
||||||
|
.open => "open",
|
||||||
|
.closed => "closed",
|
||||||
|
.filtered => "filtered",
|
||||||
|
.unfiltered => "unfiltered",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stateLabel(st: State) []const u8 {
|
||||||
|
return switch (st) {
|
||||||
|
.open => "OPEN",
|
||||||
|
.closed => "CLOSED",
|
||||||
|
.filtered => "FILTERED",
|
||||||
|
.unfiltered => "UNFILTERED",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stateColor(st: State) Rgb {
|
||||||
|
return switch (st) {
|
||||||
|
.open => neon_green,
|
||||||
|
.closed => chrome_gray,
|
||||||
|
.filtered => soft_amber,
|
||||||
|
.unfiltered => bright_white,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Dashboard = struct {
|
||||||
|
level: ColorLevel,
|
||||||
|
interactive: bool,
|
||||||
|
total: u64,
|
||||||
|
drawn: bool = false,
|
||||||
|
banners_mode: bool = false,
|
||||||
|
|
||||||
|
const body_lines: usize = 5;
|
||||||
|
|
||||||
|
pub fn init(level: ColorLevel, interactive: bool, total: u64) Dashboard {
|
||||||
|
return .{ .level = level, .interactive = interactive, .total = total };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gutter(self: *const Dashboard, out: *std.Io.Writer) !void {
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, self.level, violet_mid, gutter_bar);
|
||||||
|
try out.writeByte(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(self: *Dashboard, out: *std.Io.Writer, s: *const Stats, elapsed_ns: u64) !void {
|
||||||
|
const sent = s.sent.v.load(.monotonic);
|
||||||
|
const found = s.found.v.load(.monotonic);
|
||||||
|
const op = s.open.v.load(.monotonic);
|
||||||
|
const cl = s.closed.v.load(.monotonic);
|
||||||
|
const fi = s.filtered.v.load(.monotonic);
|
||||||
|
|
||||||
|
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000_000.0;
|
||||||
|
const pps = if (elapsed_s > 0) @as(f64, @floatFromInt(sent)) / elapsed_s else 0;
|
||||||
|
const kpps = pps / 1000.0;
|
||||||
|
const frac = if (self.total > 0) @as(f64, @floatFromInt(sent)) / @as(f64, @floatFromInt(self.total)) else 0;
|
||||||
|
const pct = frac * 100.0;
|
||||||
|
const remaining = if (self.total > sent) self.total - sent else 0;
|
||||||
|
const eta_s: u64 = if (pps > 1.0) @intFromFloat(@as(f64, @floatFromInt(remaining)) / pps) else 0;
|
||||||
|
|
||||||
|
if (!self.interactive) {
|
||||||
|
try out.print(
|
||||||
|
"[up {d:.0}s] sent {d} / {d} found {d} (open {d} closed {d} filtered {d})",
|
||||||
|
.{ elapsed_s, sent, self.total, found, op, cl, fi },
|
||||||
|
);
|
||||||
|
if (self.banners_mode) try out.print(" banners {d}", .{s.banners.v.load(.monotonic)});
|
||||||
|
try out.print(" {d:.2} kpps\n", .{kpps});
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self.drawn) try out.print("\x1b[{d}A", .{body_lines});
|
||||||
|
self.drawn = true;
|
||||||
|
|
||||||
|
try out.writeAll(clear_line);
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, self.level, violet_mid, gutter_bar);
|
||||||
|
try out.writeByte(' ');
|
||||||
|
try gradientText(out, self.level, "zingela");
|
||||||
|
try span(out, self.level, chrome_gray, " scanning");
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(clear_line);
|
||||||
|
try self.gutter(out);
|
||||||
|
try span(out, self.level, chrome_gray, "rate ");
|
||||||
|
try setFg(out, self.level, bright_white);
|
||||||
|
try out.print("{d:>8.2}", .{kpps});
|
||||||
|
try span(out, self.level, chrome_gray, " kpps ");
|
||||||
|
try progressBar(out, self.level, frac);
|
||||||
|
try out.writeByte(' ');
|
||||||
|
try setFg(out, self.level, bright_white);
|
||||||
|
try out.print("{d:>5.1}", .{pct});
|
||||||
|
try span(out, self.level, chrome_gray, "%");
|
||||||
|
try resetFg(out, self.level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(clear_line);
|
||||||
|
try self.gutter(out);
|
||||||
|
try span(out, self.level, chrome_gray, "sent ");
|
||||||
|
try setFg(out, self.level, bright_white);
|
||||||
|
try writeThousands(out, sent);
|
||||||
|
try span(out, self.level, chrome_gray, " / ");
|
||||||
|
try setFg(out, self.level, chrome_gray);
|
||||||
|
try writeThousands(out, self.total);
|
||||||
|
try resetFg(out, self.level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(clear_line);
|
||||||
|
try self.gutter(out);
|
||||||
|
try span(out, self.level, chrome_gray, "open ");
|
||||||
|
try setFg(out, self.level, neon_green);
|
||||||
|
try writeThousands(out, op);
|
||||||
|
try span(out, self.level, chrome_gray, " closed ");
|
||||||
|
try setFg(out, self.level, chrome_gray);
|
||||||
|
try writeThousands(out, cl);
|
||||||
|
try span(out, self.level, chrome_gray, " filtered ");
|
||||||
|
try setFg(out, self.level, soft_amber);
|
||||||
|
try writeThousands(out, fi);
|
||||||
|
try resetFg(out, self.level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(clear_line);
|
||||||
|
try self.gutter(out);
|
||||||
|
try span(out, self.level, chrome_gray, "found ");
|
||||||
|
try setFg(out, self.level, neon_green);
|
||||||
|
try writeThousands(out, found);
|
||||||
|
try span(out, self.level, chrome_gray, " up ");
|
||||||
|
try setFg(out, self.level, bright_white);
|
||||||
|
try writeClock(out, @intFromFloat(elapsed_s));
|
||||||
|
try span(out, self.level, chrome_gray, " eta ");
|
||||||
|
try setFg(out, self.level, bright_white);
|
||||||
|
try writeClock(out, eta_s);
|
||||||
|
try resetFg(out, self.level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn emitJson(out: *std.Io.Writer, r: Result, proto: []const u8) !void {
|
||||||
|
try out.print("{{\"ip\":\"", .{});
|
||||||
|
try writeAddr(out, r.addr);
|
||||||
|
try out.print("\",\"port\":{d},\"proto\":\"{s}\",\"state\":\"{s}\"}}\n", .{ r.port, proto, stateName(r.state) });
|
||||||
|
}
|
||||||
|
|
||||||
|
const w_host: usize = 17;
|
||||||
|
const w_port: usize = 7;
|
||||||
|
const w_state: usize = 10;
|
||||||
|
|
||||||
|
fn repeat(out: *std.Io.Writer, cell: []const u8, n: usize) !void {
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < n) : (i += 1) try out.writeAll(cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rule(out: *std.Io.Writer, level: ColorLevel, hw: usize, left: []const u8, mid: []const u8, right: []const u8) !void {
|
||||||
|
try setFg(out, level, chrome_gray);
|
||||||
|
try out.writeAll(left);
|
||||||
|
try repeat(out, box_h, hw + 2);
|
||||||
|
try out.writeAll(mid);
|
||||||
|
try repeat(out, box_h, w_port + 2);
|
||||||
|
try out.writeAll(mid);
|
||||||
|
try repeat(out, box_h, w_state + 2);
|
||||||
|
try out.writeAll(right);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pad(out: *std.Io.Writer, n: usize) !void {
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < n) : (i += 1) try out.writeByte(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renderTable(out: *std.Io.Writer, level: ColorLevel, results: []const Result) !void {
|
||||||
|
var hw: usize = "HOST".len;
|
||||||
|
for (results) |r| hw = @max(hw, addrWidth(r.addr));
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try rule(out, level, hw, "\u{250c}", "\u{252c}", "\u{2510}");
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, chrome_gray, "\u{2502} ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll("HOST");
|
||||||
|
try pad(out, hw - "HOST".len);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try pad(out, w_port - "PORT".len);
|
||||||
|
try out.writeAll("PORT");
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll("STATE");
|
||||||
|
try pad(out, w_state - "STATE".len);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502}");
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try rule(out, level, hw, "\u{251c}", "\u{253c}", "\u{2524}");
|
||||||
|
|
||||||
|
for (results) |r| {
|
||||||
|
var ipbuf: [max_addr_str]u8 = undefined;
|
||||||
|
var ipw = std.Io.Writer.fixed(&ipbuf);
|
||||||
|
try writeAddr(&ipw, r.addr);
|
||||||
|
const ip_str = ipbuf[0..ipw.end];
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, chrome_gray, "\u{2502} ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll(ip_str);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try pad(out, hw - ip_str.len);
|
||||||
|
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
var portbuf: [5]u8 = undefined;
|
||||||
|
const port_str = std.fmt.bufPrint(&portbuf, "{d}", .{r.port}) catch unreachable;
|
||||||
|
try pad(out, w_port - port_str.len);
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll(port_str);
|
||||||
|
try resetFg(out, level);
|
||||||
|
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
const label = stateLabel(r.state);
|
||||||
|
try setFg(out, level, stateColor(r.state));
|
||||||
|
try out.writeAll(label);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try pad(out, w_state - label.len);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502}");
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try rule(out, level, hw, "\u{2514}", "\u{2534}", "\u{2518}");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ipPortLess(_: void, a: Result, b: Result) bool {
|
||||||
|
return switch (packet.Addr.order(a.addr, b.addr)) {
|
||||||
|
.lt => true,
|
||||||
|
.gt => false,
|
||||||
|
.eq => a.port < b.port,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const ServiceRow = struct {
|
||||||
|
ip: u32,
|
||||||
|
port: u16,
|
||||||
|
service: []const u8,
|
||||||
|
info: []const u8,
|
||||||
|
tls: bool,
|
||||||
|
};
|
||||||
|
|
||||||
|
const w_service: usize = 11;
|
||||||
|
const w_info: usize = 34;
|
||||||
|
|
||||||
|
fn serviceRule(out: *std.Io.Writer, level: ColorLevel, left: []const u8, mid: []const u8, right: []const u8) !void {
|
||||||
|
try setFg(out, level, chrome_gray);
|
||||||
|
try out.writeAll(left);
|
||||||
|
try repeat(out, box_h, w_host + 2);
|
||||||
|
try out.writeAll(mid);
|
||||||
|
try repeat(out, box_h, w_port + 2);
|
||||||
|
try out.writeAll(mid);
|
||||||
|
try repeat(out, box_h, w_service + 2);
|
||||||
|
try out.writeAll(mid);
|
||||||
|
try repeat(out, box_h, w_info + 2);
|
||||||
|
try out.writeAll(right);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
fn renderCell(out: *std.Io.Writer, level: ColorLevel, text: []const u8, width: usize, color: Rgb) !void {
|
||||||
|
const n = @min(text.len, width);
|
||||||
|
try setFg(out, level, color);
|
||||||
|
try out.writeAll(text[0..n]);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try pad(out, width - n);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renderServices(out: *std.Io.Writer, level: ColorLevel, rows: []const ServiceRow) !void {
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try serviceRule(out, level, "\u{250c}", "\u{252c}", "\u{2510}");
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, chrome_gray, "\u{2502} ");
|
||||||
|
try renderCell(out, level, "HOST", w_host, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try renderCell(out, level, "PORT", w_port, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try renderCell(out, level, "SERVICE", w_service, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try renderCell(out, level, "VERSION / INFO", w_info, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502}");
|
||||||
|
try out.writeByte('\n');
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try serviceRule(out, level, "\u{251c}", "\u{253c}", "\u{2524}");
|
||||||
|
|
||||||
|
for (rows) |r| {
|
||||||
|
var ipbuf: [15]u8 = undefined;
|
||||||
|
var ipw = std.Io.Writer.fixed(&ipbuf);
|
||||||
|
try writeIp(&ipw, r.ip);
|
||||||
|
|
||||||
|
var portbuf: [5]u8 = undefined;
|
||||||
|
const port_str = std.fmt.bufPrint(&portbuf, "{d}", .{r.port}) catch unreachable;
|
||||||
|
|
||||||
|
const info_disp = if (r.info.len > 0) r.info else if (r.tls) "(encrypted)" else "";
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, chrome_gray, "\u{2502} ");
|
||||||
|
try renderCell(out, level, ipbuf[0..ipw.end], w_host, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try pad(out, w_port - port_str.len);
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll(port_str);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try renderCell(out, level, r.service, w_service, neon_green);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502} ");
|
||||||
|
try renderCell(out, level, info_disp, w_info, bright_white);
|
||||||
|
try span(out, level, chrome_gray, " \u{2502}");
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try serviceRule(out, level, "\u{2514}", "\u{2534}", "\u{2518}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn jsonEscape(out: *std.Io.Writer, s: []const u8) !void {
|
||||||
|
for (s) |c| switch (c) {
|
||||||
|
'"' => try out.writeAll("\\\""),
|
||||||
|
'\\' => try out.writeAll("\\\\"),
|
||||||
|
else => try out.writeByte(c),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn emitServiceJson(out: *std.Io.Writer, row: ServiceRow) !void {
|
||||||
|
try out.writeAll("{\"ip\":\"");
|
||||||
|
try writeIp(out, row.ip);
|
||||||
|
try out.print("\",\"port\":{d},\"service\":\"", .{row.port});
|
||||||
|
try jsonEscape(out, row.service);
|
||||||
|
try out.writeAll("\",\"info\":\"");
|
||||||
|
try jsonEscape(out, row.info);
|
||||||
|
try out.print("\",\"tls\":{s}}}\n", .{if (row.tls) "true" else "false"});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renderSummary(
|
||||||
|
out: *std.Io.Writer,
|
||||||
|
level: ColorLevel,
|
||||||
|
sent: u64,
|
||||||
|
probe: []const u8,
|
||||||
|
ifname: []const u8,
|
||||||
|
elapsed_s: f64,
|
||||||
|
open: u64,
|
||||||
|
closed: u64,
|
||||||
|
filtered: u64,
|
||||||
|
unfiltered: u64,
|
||||||
|
banners: u64,
|
||||||
|
) !void {
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, violet_mid, gutter_bar);
|
||||||
|
try out.writeByte(' ');
|
||||||
|
try span(out, level, chrome_gray, "sent ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try writeThousands(out, sent);
|
||||||
|
try setFg(out, level, chrome_gray);
|
||||||
|
try out.print(" {s} on ", .{probe});
|
||||||
|
try resetFg(out, level);
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.writeAll(ifname);
|
||||||
|
try span(out, level, chrome_gray, " in ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try out.print("{d:.3}s", .{elapsed_s});
|
||||||
|
try span(out, level, chrome_gray, " \u{2192} ");
|
||||||
|
try setFg(out, level, neon_green);
|
||||||
|
try writeThousands(out, open);
|
||||||
|
try span(out, level, chrome_gray, " open ");
|
||||||
|
try setFg(out, level, chrome_gray);
|
||||||
|
try writeThousands(out, closed);
|
||||||
|
try span(out, level, chrome_gray, " closed ");
|
||||||
|
try setFg(out, level, soft_amber);
|
||||||
|
try writeThousands(out, filtered);
|
||||||
|
try span(out, level, chrome_gray, " filtered");
|
||||||
|
if (unfiltered > 0) {
|
||||||
|
try span(out, level, chrome_gray, " ");
|
||||||
|
try setFg(out, level, bright_white);
|
||||||
|
try writeThousands(out, unfiltered);
|
||||||
|
try span(out, level, chrome_gray, " unfiltered");
|
||||||
|
}
|
||||||
|
if (banners > 0) {
|
||||||
|
try span(out, level, chrome_gray, " \u{2192} ");
|
||||||
|
try setFg(out, level, neon_green);
|
||||||
|
try writeThousands(out, banners);
|
||||||
|
try span(out, level, chrome_gray, " banners");
|
||||||
|
}
|
||||||
|
try resetFg(out, level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn renderUnanswered(out: *std.Io.Writer, level: ColorLevel, count: u64) !void {
|
||||||
|
try out.writeAll(" ");
|
||||||
|
try span(out, level, violet_mid, gutter_bar);
|
||||||
|
try out.writeByte(' ');
|
||||||
|
try span(out, level, chrome_gray, "open|filtered (no response) ");
|
||||||
|
try setFg(out, level, soft_amber);
|
||||||
|
try writeThousands(out, count);
|
||||||
|
try resetFg(out, level);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
test "renderUnanswered reports the silent-port count in plain mode" {
|
||||||
|
var buf: [128]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try renderUnanswered(&w, .none, 4094);
|
||||||
|
const text = buf[0..w.end];
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "open|filtered (no response)") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "4,094") != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "resolveLevel honors choice, tty state, and truecolor" {
|
||||||
|
try std.testing.expectEqual(ColorLevel.none, resolveLevel(.never, true, true));
|
||||||
|
try std.testing.expectEqual(ColorLevel.truecolor, resolveLevel(.always, false, true));
|
||||||
|
try std.testing.expectEqual(ColorLevel.ansi256, resolveLevel(.always, false, false));
|
||||||
|
try std.testing.expectEqual(ColorLevel.none, resolveLevel(.auto, false, true));
|
||||||
|
try std.testing.expectEqual(ColorLevel.truecolor, resolveLevel(.auto, true, true));
|
||||||
|
try std.testing.expectEqual(ColorLevel.ansi256, resolveLevel(.auto, true, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseColorChoice maps flag values" {
|
||||||
|
try std.testing.expectEqual(ColorChoice.auto, parseColorChoice(null));
|
||||||
|
try std.testing.expectEqual(ColorChoice.always, parseColorChoice("always"));
|
||||||
|
try std.testing.expectEqual(ColorChoice.never, parseColorChoice("never"));
|
||||||
|
try std.testing.expectEqual(ColorChoice.auto, parseColorChoice("garbage"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "to256 maps palette anchors into the 6x6x6 cube" {
|
||||||
|
try std.testing.expectEqual(@as(u8, 16), to256(.{ .r = 0, .g = 0, .b = 0 }));
|
||||||
|
try std.testing.expectEqual(@as(u8, 231), to256(.{ .r = 255, .g = 255, .b = 255 }));
|
||||||
|
try std.testing.expect(to256(neon_green) >= 16 and to256(neon_green) <= 231);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "violetAt interpolates the gradient endpoints and midpoint" {
|
||||||
|
try std.testing.expectEqual(violet_light, violetAt(0.0));
|
||||||
|
try std.testing.expectEqual(violet_deep, violetAt(1.0));
|
||||||
|
try std.testing.expectEqual(violet_mid, violetAt(0.5));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Padded counter is cache-line sized and aligned to prevent false sharing" {
|
||||||
|
try std.testing.expectEqual(@as(usize, cache_line), @sizeOf(Padded));
|
||||||
|
try std.testing.expectEqual(@as(usize, cache_line), @alignOf(Padded));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Stats.record tallies per-state and total found without cross-talk" {
|
||||||
|
var s: Stats = .{};
|
||||||
|
s.record(.open);
|
||||||
|
s.record(.open);
|
||||||
|
s.record(.closed);
|
||||||
|
s.record(.filtered);
|
||||||
|
try std.testing.expectEqual(@as(u64, 2), s.open.v.load(.monotonic));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), s.closed.v.load(.monotonic));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), s.filtered.v.load(.monotonic));
|
||||||
|
try std.testing.expectEqual(@as(u64, 4), s.found.v.load(.monotonic));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), s.sent.v.load(.monotonic));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "emitJson writes one greppable NDJSON object per result with its proto" {
|
||||||
|
var buf: [128]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try emitJson(&w, Result.v4(0x0a000005, 80, .open), "tcp");
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
"{\"ip\":\"10.0.0.5\",\"port\":80,\"proto\":\"tcp\",\"state\":\"open\"}\n",
|
||||||
|
buf[0..w.end],
|
||||||
|
);
|
||||||
|
w = std.Io.Writer.fixed(&buf);
|
||||||
|
try emitJson(&w, Result.v4(0x08080808, 53, .closed), "udp");
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
"{\"ip\":\"8.8.8.8\",\"port\":53,\"proto\":\"udp\",\"state\":\"closed\"}\n",
|
||||||
|
buf[0..w.end],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "writeThousands groups digits and leaves small numbers intact" {
|
||||||
|
var buf: [32]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try writeThousands(&w, 1234567);
|
||||||
|
try std.testing.expectEqualStrings("1,234,567", buf[0..w.end]);
|
||||||
|
w = std.Io.Writer.fixed(&buf);
|
||||||
|
try writeThousands(&w, 42);
|
||||||
|
try std.testing.expectEqualStrings("42", buf[0..w.end]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "renderTable with no color is plain and contains every host row" {
|
||||||
|
var buf: [1024]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
const rows = [_]Result{
|
||||||
|
Result.v4(0x0a000005, 80, .open),
|
||||||
|
Result.v4(0x0a000006, 443, .closed),
|
||||||
|
};
|
||||||
|
try renderTable(&w, .none, &rows);
|
||||||
|
const text = buf[0..w.end];
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "10.0.0.5") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "OPEN") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "10.0.0.6") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "CLOSED") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, esc) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "dashboard non-interactive frame is a single plain line" {
|
||||||
|
var buf: [256]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
var s: Stats = .{};
|
||||||
|
_ = s.sent.v.fetchAdd(500, .monotonic);
|
||||||
|
s.record(.open);
|
||||||
|
var dash = Dashboard.init(.none, false, 1000);
|
||||||
|
try dash.render(&w, &s, 1_000_000_000);
|
||||||
|
const text = buf[0..w.end];
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "sent 500 / 1000") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, esc) == null);
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, "\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "writeIp6 renders RFC 5952 compressed IPv6 (longest-first run, no single-group elision)" {
|
||||||
|
const cases = .{
|
||||||
|
.{ [16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, "::" },
|
||||||
|
.{ [16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, "::1" },
|
||||||
|
.{ [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, "2001:db8::1" },
|
||||||
|
.{ [16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, "fe80::" },
|
||||||
|
.{ [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, "2001:db8:1::1" },
|
||||||
|
.{ [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6 }, "2001:db8:1:2:3:4:5:6" },
|
||||||
|
.{ [16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xc0, 0xa8, 0, 1 }, "::ffff:c0a8:1" },
|
||||||
|
};
|
||||||
|
inline for (cases) |c| {
|
||||||
|
var buf: [max_addr_str]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try writeIp6(&w, c[0]);
|
||||||
|
try std.testing.expectEqualStrings(c[1], buf[0..w.end]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "emitJson renders an IPv6 result address" {
|
||||||
|
var buf: [128]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
const a = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||||
|
try emitJson(&w, Result.v6(a, 443, .open), "tcp");
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
"{\"ip\":\"2001:db8::1\",\"port\":443,\"proto\":\"tcp\",\"state\":\"open\"}\n",
|
||||||
|
buf[0..w.end],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ipPortLess orders by ip then port" {
|
||||||
|
const a = Result.v4(0x0a000001, 443, .open);
|
||||||
|
const b = Result.v4(0x0a000001, 80, .open);
|
||||||
|
const c = Result.v4(0x0a000002, 1, .open);
|
||||||
|
try std.testing.expect(ipPortLess({}, b, a));
|
||||||
|
try std.testing.expect(ipPortLess({}, a, c));
|
||||||
|
try std.testing.expect(!ipPortLess({}, a, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "emitServiceJson emits an escaped, greppable service record" {
|
||||||
|
var buf: [256]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try emitServiceJson(&w, .{ .ip = 0x0a000005, .port = 22, .service = "ssh", .info = "OpenSSH_9.6p1", .tls = false });
|
||||||
|
try std.testing.expectEqualStrings(
|
||||||
|
"{\"ip\":\"10.0.0.5\",\"port\":22,\"service\":\"ssh\",\"info\":\"OpenSSH_9.6p1\",\"tls\":false}\n",
|
||||||
|
buf[0..w.end],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "emitServiceJson escapes quotes and backslashes in the info field" {
|
||||||
|
var buf: [256]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
try emitServiceJson(&w, .{ .ip = 0x0a000006, .port = 443, .service = "ssl/tls", .info = "a\"b\\c", .tls = true });
|
||||||
|
const text = buf[0..w.end];
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "\\\"") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "\\\\") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "\"tls\":true") != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "renderServices lists each host, service, and version without escapes in plain mode" {
|
||||||
|
var buf: [4096]u8 = undefined;
|
||||||
|
var w = std.Io.Writer.fixed(&buf);
|
||||||
|
const rows = [_]ServiceRow{
|
||||||
|
.{ .ip = 0x0a000005, .port = 22, .service = "ssh", .info = "OpenSSH_9.6p1", .tls = false },
|
||||||
|
.{ .ip = 0x0a000006, .port = 443, .service = "ssl/tls", .info = "", .tls = true },
|
||||||
|
};
|
||||||
|
try renderServices(&w, .none, &rows);
|
||||||
|
const text = buf[0..w.end];
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "10.0.0.5") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "ssh") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "OpenSSH_9.6p1") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "ssl/tls") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, "(encrypted)") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, text, esc) == null);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,647 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// packet.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
|
pub const EthHdr = extern struct {
|
||||||
|
dst: [6]u8,
|
||||||
|
src: [6]u8,
|
||||||
|
ethertype: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Ipv4Hdr = extern struct {
|
||||||
|
version_ihl: u8,
|
||||||
|
tos: u8,
|
||||||
|
total_len: u16,
|
||||||
|
id: u16,
|
||||||
|
flags_frag: u16,
|
||||||
|
ttl: u8,
|
||||||
|
protocol: u8,
|
||||||
|
checksum: u16,
|
||||||
|
src: u32,
|
||||||
|
dst: u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const TcpHdr = extern struct {
|
||||||
|
src_port: u16,
|
||||||
|
dst_port: u16,
|
||||||
|
seq: u32,
|
||||||
|
ack: u32,
|
||||||
|
data_off_ns: u8,
|
||||||
|
flags: u8,
|
||||||
|
window: u16,
|
||||||
|
checksum: u16,
|
||||||
|
urgent: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const UdpHdr = extern struct {
|
||||||
|
src_port: u16,
|
||||||
|
dst_port: u16,
|
||||||
|
length: u16,
|
||||||
|
checksum: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Ipv6Hdr = extern struct {
|
||||||
|
version_tc_flow: u32,
|
||||||
|
payload_len: u16,
|
||||||
|
next_header: u8,
|
||||||
|
hop_limit: u8,
|
||||||
|
src: [16]u8,
|
||||||
|
dst: [16]u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(@sizeOf(EthHdr) == 14);
|
||||||
|
std.debug.assert(@sizeOf(Ipv4Hdr) == 20);
|
||||||
|
std.debug.assert(@sizeOf(TcpHdr) == 20);
|
||||||
|
std.debug.assert(@sizeOf(UdpHdr) == 8);
|
||||||
|
std.debug.assert(@sizeOf(Ipv6Hdr) == 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Addr = union(enum) {
|
||||||
|
v4: u32,
|
||||||
|
v6: [16]u8,
|
||||||
|
|
||||||
|
pub fn eql(a: Addr, b: Addr) bool {
|
||||||
|
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
|
||||||
|
return switch (a) {
|
||||||
|
.v4 => |x| x == b.v4,
|
||||||
|
.v6 => |x| std.mem.eql(u8, &x, &b.v6),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn order(a: Addr, b: Addr) std.math.Order {
|
||||||
|
const fam_a: u2 = if (a == .v4) 0 else 1;
|
||||||
|
const fam_b: u2 = if (b == .v4) 0 else 1;
|
||||||
|
if (fam_a != fam_b) return std.math.order(fam_a, fam_b);
|
||||||
|
return switch (a) {
|
||||||
|
.v4 => |x| std.math.order(x, b.v4),
|
||||||
|
.v6 => |x| std.mem.order(u8, &x, &b.v6),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn checksum(bytes: []const u8) u16 {
|
||||||
|
var sum: u32 = 0;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + 1 < bytes.len) : (i += 2) {
|
||||||
|
const word = (@as(u16, bytes[i]) << 8) | @as(u16, bytes[i + 1]);
|
||||||
|
sum += word;
|
||||||
|
}
|
||||||
|
if (i < bytes.len) {
|
||||||
|
sum += @as(u32, bytes[i]) << 8;
|
||||||
|
}
|
||||||
|
while (sum >> 16 != 0) {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
}
|
||||||
|
return ~@as(u16, @truncate(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn checksumSimd(bytes: []const u8) u16 {
|
||||||
|
const lanes = comptime (std.simd.suggestVectorLength(u16) orelse 8);
|
||||||
|
const stride = lanes * 2;
|
||||||
|
const native_le = builtin.cpu.arch.endian() == .little;
|
||||||
|
|
||||||
|
var acc: @Vector(lanes, u32) = @splat(0);
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + stride <= bytes.len) : (i += stride) {
|
||||||
|
const block: [stride]u8 = bytes[i..][0..stride].*;
|
||||||
|
var words: @Vector(lanes, u16) = @bitCast(block);
|
||||||
|
if (native_le) words = @byteSwap(words);
|
||||||
|
acc += @as(@Vector(lanes, u32), words);
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum: u32 = @reduce(.Add, acc);
|
||||||
|
while (i + 1 < bytes.len) : (i += 2) {
|
||||||
|
sum += (@as(u32, bytes[i]) << 8) | @as(u32, bytes[i + 1]);
|
||||||
|
}
|
||||||
|
if (i < bytes.len) {
|
||||||
|
sum += @as(u32, bytes[i]) << 8;
|
||||||
|
}
|
||||||
|
while (sum >> 16 != 0) {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
}
|
||||||
|
return ~@as(u16, @truncate(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn incrementalUpdate(old_check: u16, old_word: u16, new_word: u16) u16 {
|
||||||
|
var sum: u32 = @as(u32, ~old_check) + @as(u32, ~old_word) + @as(u32, new_word);
|
||||||
|
while (sum >> 16 != 0) {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
}
|
||||||
|
return ~@as(u16, @truncate(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tcpChecksum(src_be: u32, dst_be: u32, segment: []const u8) u16 {
|
||||||
|
var pseudo: [12]u8 = undefined;
|
||||||
|
@memcpy(pseudo[0..4], std.mem.asBytes(&src_be));
|
||||||
|
@memcpy(pseudo[4..8], std.mem.asBytes(&dst_be));
|
||||||
|
pseudo[8] = 0;
|
||||||
|
pseudo[9] = 6;
|
||||||
|
std.mem.writeInt(u16, pseudo[10..12], @intCast(segment.len), .big);
|
||||||
|
|
||||||
|
var sum: u32 = 0;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + 1 < pseudo.len) : (i += 2) {
|
||||||
|
sum += (@as(u32, pseudo[i]) << 8) | @as(u32, pseudo[i + 1]);
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i + 1 < segment.len) : (i += 2) {
|
||||||
|
sum += (@as(u32, segment[i]) << 8) | @as(u32, segment[i + 1]);
|
||||||
|
}
|
||||||
|
if (i < segment.len) {
|
||||||
|
sum += @as(u32, segment[i]) << 8;
|
||||||
|
}
|
||||||
|
while (sum >> 16 != 0) {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
}
|
||||||
|
return ~@as(u16, @truncate(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pseudoChecksum6(src: [16]u8, dst: [16]u8, next_header: u8, payload: []const u8) u16 {
|
||||||
|
var sum: u32 = 0;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + 1 < src.len) : (i += 2) sum += (@as(u32, src[i]) << 8) | src[i + 1];
|
||||||
|
i = 0;
|
||||||
|
while (i + 1 < dst.len) : (i += 2) sum += (@as(u32, dst[i]) << 8) | dst[i + 1];
|
||||||
|
const len: u32 = @intCast(payload.len);
|
||||||
|
sum += (len >> 16) & 0xffff;
|
||||||
|
sum += len & 0xffff;
|
||||||
|
sum += next_header;
|
||||||
|
i = 0;
|
||||||
|
while (i + 1 < payload.len) : (i += 2) sum += (@as(u32, payload[i]) << 8) | payload[i + 1];
|
||||||
|
if (i < payload.len) sum += @as(u32, payload[i]) << 8;
|
||||||
|
while (sum >> 16 != 0) sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
return ~@as(u16, @truncate(sum));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tcpChecksum6(src: [16]u8, dst: [16]u8, segment: []const u8) u16 {
|
||||||
|
return pseudoChecksum6(src, dst, 6, segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn udpChecksum(src_be: u32, dst_be: u32, segment: []const u8) u16 {
|
||||||
|
var pseudo: [12]u8 = undefined;
|
||||||
|
@memcpy(pseudo[0..4], std.mem.asBytes(&src_be));
|
||||||
|
@memcpy(pseudo[4..8], std.mem.asBytes(&dst_be));
|
||||||
|
pseudo[8] = 0;
|
||||||
|
pseudo[9] = 17;
|
||||||
|
std.mem.writeInt(u16, pseudo[10..12], @intCast(segment.len), .big);
|
||||||
|
|
||||||
|
var sum: u32 = 0;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i + 1 < pseudo.len) : (i += 2) {
|
||||||
|
sum += (@as(u32, pseudo[i]) << 8) | @as(u32, pseudo[i + 1]);
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
while (i + 1 < segment.len) : (i += 2) {
|
||||||
|
sum += (@as(u32, segment[i]) << 8) | @as(u32, segment[i + 1]);
|
||||||
|
}
|
||||||
|
if (i < segment.len) {
|
||||||
|
sum += @as(u32, segment[i]) << 8;
|
||||||
|
}
|
||||||
|
while (sum >> 16 != 0) {
|
||||||
|
sum = (sum & 0xffff) + (sum >> 16);
|
||||||
|
}
|
||||||
|
const folded: u16 = ~@as(u16, @truncate(sum));
|
||||||
|
return if (folded == 0) 0xffff else folded;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TcpFlag = struct {
|
||||||
|
pub const fin: u8 = 0x01;
|
||||||
|
pub const syn: u8 = 0x02;
|
||||||
|
pub const rst: u8 = 0x04;
|
||||||
|
pub const psh: u8 = 0x08;
|
||||||
|
pub const ack: u8 = 0x10;
|
||||||
|
pub const urg: u8 = 0x20;
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const ScanType = enum {
|
||||||
|
syn,
|
||||||
|
fin,
|
||||||
|
null_scan,
|
||||||
|
xmas,
|
||||||
|
maimon,
|
||||||
|
ack,
|
||||||
|
window,
|
||||||
|
|
||||||
|
pub fn probeFlags(self: ScanType) u8 {
|
||||||
|
return switch (self) {
|
||||||
|
.syn => TcpFlag.syn,
|
||||||
|
.fin => TcpFlag.fin,
|
||||||
|
.null_scan => 0,
|
||||||
|
.xmas => TcpFlag.fin | TcpFlag.psh | TcpFlag.urg,
|
||||||
|
.maimon => TcpFlag.fin | TcpFlag.ack,
|
||||||
|
.ack, .window => TcpFlag.ack,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cookieInAck(self: ScanType) bool {
|
||||||
|
return switch (self) {
|
||||||
|
.maimon, .ack, .window => true,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seqConsumed(self: ScanType) u32 {
|
||||||
|
return switch (self) {
|
||||||
|
.syn, .fin, .xmas => 1,
|
||||||
|
else => 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(text: []const u8) ?ScanType {
|
||||||
|
const map = .{
|
||||||
|
.{ "syn", ScanType.syn },
|
||||||
|
.{ "fin", ScanType.fin },
|
||||||
|
.{ "null", ScanType.null_scan },
|
||||||
|
.{ "xmas", ScanType.xmas },
|
||||||
|
.{ "maimon", ScanType.maimon },
|
||||||
|
.{ "ack", ScanType.ack },
|
||||||
|
.{ "window", ScanType.window },
|
||||||
|
};
|
||||||
|
inline for (map) |entry| {
|
||||||
|
if (std.mem.eql(u8, text, entry[0])) return entry[1];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const opt_eol: u8 = 0;
|
||||||
|
const opt_nop: u8 = 1;
|
||||||
|
const opt_mss: u8 = 2;
|
||||||
|
const opt_mss_len: u8 = 4;
|
||||||
|
const opt_wscale: u8 = 3;
|
||||||
|
const opt_wscale_len: u8 = 3;
|
||||||
|
const opt_sack_perm: u8 = 4;
|
||||||
|
const opt_sack_perm_len: u8 = 2;
|
||||||
|
const opt_ts: u8 = 8;
|
||||||
|
const opt_ts_len: u8 = 10;
|
||||||
|
|
||||||
|
const mss_ethernet_hi: u8 = 0x05;
|
||||||
|
const mss_ethernet_lo: u8 = 0xb4;
|
||||||
|
|
||||||
|
const wscale_linux: u8 = 7;
|
||||||
|
const wscale_windows: u8 = 8;
|
||||||
|
const wscale_macos: u8 = 6;
|
||||||
|
|
||||||
|
const window_minimal: u16 = 1024;
|
||||||
|
const window_linux: u16 = 64240;
|
||||||
|
const window_windows: u16 = 64240;
|
||||||
|
const window_macos: u16 = 65535;
|
||||||
|
|
||||||
|
const syn_opts_masscan = [_]u8{ opt_mss, opt_mss_len, mss_ethernet_hi, mss_ethernet_lo };
|
||||||
|
|
||||||
|
const syn_opts_linux = [_]u8{
|
||||||
|
opt_mss, opt_mss_len, mss_ethernet_hi, mss_ethernet_lo,
|
||||||
|
opt_sack_perm, opt_sack_perm_len, opt_ts, opt_ts_len,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
opt_nop, opt_wscale, opt_wscale_len, wscale_linux,
|
||||||
|
};
|
||||||
|
|
||||||
|
const syn_opts_windows = [_]u8{
|
||||||
|
opt_mss, opt_mss_len, mss_ethernet_hi, mss_ethernet_lo,
|
||||||
|
opt_nop, opt_wscale, opt_wscale_len, wscale_windows,
|
||||||
|
opt_nop, opt_nop, opt_sack_perm, opt_sack_perm_len,
|
||||||
|
};
|
||||||
|
|
||||||
|
const syn_opts_macos = [_]u8{
|
||||||
|
opt_mss, opt_mss_len, mss_ethernet_hi, mss_ethernet_lo,
|
||||||
|
opt_nop, opt_wscale, opt_wscale_len, wscale_macos,
|
||||||
|
opt_nop, opt_nop, opt_ts, opt_ts_len,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
opt_sack_perm, opt_sack_perm_len, opt_eol, opt_eol,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const max_syn_options_len: usize = syn_opts_macos.len;
|
||||||
|
|
||||||
|
pub const OsProfile = enum {
|
||||||
|
none,
|
||||||
|
masscan,
|
||||||
|
linux,
|
||||||
|
windows,
|
||||||
|
macos,
|
||||||
|
|
||||||
|
pub fn options(self: OsProfile) []const u8 {
|
||||||
|
return switch (self) {
|
||||||
|
.none => &.{},
|
||||||
|
.masscan => &syn_opts_masscan,
|
||||||
|
.linux => &syn_opts_linux,
|
||||||
|
.windows => &syn_opts_windows,
|
||||||
|
.macos => &syn_opts_macos,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn window(self: OsProfile) u16 {
|
||||||
|
return switch (self) {
|
||||||
|
.none, .masscan => window_minimal,
|
||||||
|
.linux => window_linux,
|
||||||
|
.windows => window_windows,
|
||||||
|
.macos => window_macos,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tsValOffset(self: OsProfile) ?usize {
|
||||||
|
return switch (self) {
|
||||||
|
.linux => 8,
|
||||||
|
.macos => 12,
|
||||||
|
else => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variesIpId(self: OsProfile) bool {
|
||||||
|
return switch (self) {
|
||||||
|
.windows, .macos => true,
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(text: []const u8) ?OsProfile {
|
||||||
|
const map = .{
|
||||||
|
.{ "none", OsProfile.none },
|
||||||
|
.{ "masscan", OsProfile.masscan },
|
||||||
|
.{ "linux", OsProfile.linux },
|
||||||
|
.{ "windows", OsProfile.windows },
|
||||||
|
.{ "macos", OsProfile.macos },
|
||||||
|
};
|
||||||
|
inline for (map) |entry| {
|
||||||
|
if (std.mem.eql(u8, text, entry[0])) return entry[1];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn optionKinds(opts: []const u8, out: []u8) usize {
|
||||||
|
var i: usize = 0;
|
||||||
|
var n: usize = 0;
|
||||||
|
while (i < opts.len) {
|
||||||
|
const kind = opts[i];
|
||||||
|
if (kind == opt_eol) break;
|
||||||
|
if (n < out.len) {
|
||||||
|
out[n] = kind;
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
if (kind == opt_nop) {
|
||||||
|
i += 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (i + 1 >= opts.len) break;
|
||||||
|
const len = opts[i + 1];
|
||||||
|
if (len < 2) break;
|
||||||
|
i += len;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert((@sizeOf(TcpHdr) + syn_opts_masscan.len) % 4 == 0);
|
||||||
|
std.debug.assert((@sizeOf(TcpHdr) + syn_opts_linux.len) % 4 == 0);
|
||||||
|
std.debug.assert((@sizeOf(TcpHdr) + syn_opts_windows.len) % 4 == 0);
|
||||||
|
std.debug.assert((@sizeOf(TcpHdr) + syn_opts_macos.len) % 4 == 0);
|
||||||
|
std.debug.assert(syn_opts_linux.len == 20);
|
||||||
|
std.debug.assert(syn_opts_windows.len == 12);
|
||||||
|
std.debug.assert(syn_opts_macos.len == 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "header sizes are wire-exact" {
|
||||||
|
try std.testing.expectEqual(@as(usize, 14), @sizeOf(EthHdr));
|
||||||
|
try std.testing.expectEqual(@as(usize, 20), @sizeOf(Ipv4Hdr));
|
||||||
|
try std.testing.expectEqual(@as(usize, 20), @sizeOf(TcpHdr));
|
||||||
|
try std.testing.expectEqual(@as(usize, 8), @sizeOf(UdpHdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "RFC 1071 checksum matches the canonical IPv4 KAT (0xb861)" {
|
||||||
|
const hdr = [_]u8{
|
||||||
|
0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, 0x00,
|
||||||
|
0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01,
|
||||||
|
0xc0, 0xa8, 0x00, 0xc7,
|
||||||
|
};
|
||||||
|
try std.testing.expectEqual(@as(u16, 0xb861), checksum(&hdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SIMD checksum matches the canonical IPv4 KAT (0xb861)" {
|
||||||
|
const hdr = [_]u8{
|
||||||
|
0x45, 0x00, 0x00, 0x73, 0x00, 0x00, 0x40, 0x00,
|
||||||
|
0x40, 0x11, 0x00, 0x00, 0xc0, 0xa8, 0x00, 0x01,
|
||||||
|
0xc0, 0xa8, 0x00, 0xc7,
|
||||||
|
};
|
||||||
|
try std.testing.expectEqual(@as(u16, 0xb861), checksumSimd(&hdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SIMD checksum equals scalar checksum for every length 0..256" {
|
||||||
|
var prng = std.Random.DefaultPrng.init(0xC0FFEE_1624_517A);
|
||||||
|
const rand = prng.random();
|
||||||
|
var buf: [256]u8 = undefined;
|
||||||
|
var len: usize = 0;
|
||||||
|
while (len <= 256) : (len += 1) {
|
||||||
|
rand.bytes(buf[0..len]);
|
||||||
|
try std.testing.expectEqual(checksum(buf[0..len]), checksumSimd(buf[0..len]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "RFC 1624 incremental update matches the RFC section 4 worked example" {
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x0000), incrementalUpdate(0xDD2F, 0x5555, 0x3285));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "incremental update equals a full recompute for random single-word edits" {
|
||||||
|
var prng = std.Random.DefaultPrng.init(0x1624_DEAD_BEEF_0001);
|
||||||
|
const rand = prng.random();
|
||||||
|
var hdr: [20]u8 = undefined;
|
||||||
|
var trial: usize = 0;
|
||||||
|
while (trial < 4096) : (trial += 1) {
|
||||||
|
rand.bytes(&hdr);
|
||||||
|
std.mem.writeInt(u16, hdr[10..12], 0, .big);
|
||||||
|
const old_check = checksum(&hdr);
|
||||||
|
const word_index = rand.uintLessThan(usize, 9) * 2;
|
||||||
|
const off = if (word_index >= 10) word_index + 2 else word_index;
|
||||||
|
const old_word = std.mem.readInt(u16, hdr[off..][0..2], .big);
|
||||||
|
const new_word = rand.int(u16);
|
||||||
|
std.mem.writeInt(u16, hdr[off..][0..2], new_word, .big);
|
||||||
|
const full = checksum(&hdr);
|
||||||
|
try std.testing.expectEqual(full, incrementalUpdate(old_check, old_word, new_word));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "tcpChecksum self-verifies: a segment with its correct checksum folds to 0" {
|
||||||
|
var tcp = TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, 54321),
|
||||||
|
.dst_port = std.mem.nativeToBig(u16, 80),
|
||||||
|
.seq = std.mem.nativeToBig(u32, 0xdead_beef),
|
||||||
|
.ack = 0,
|
||||||
|
.data_off_ns = 0x50,
|
||||||
|
.flags = 0x02,
|
||||||
|
.window = std.mem.nativeToBig(u16, 1024),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
const src = std.mem.nativeToBig(u32, 0x7f000001);
|
||||||
|
const dst = std.mem.nativeToBig(u32, 0x7f000001);
|
||||||
|
tcp.checksum = std.mem.nativeToBig(u16, tcpChecksum(src, dst, std.mem.asBytes(&tcp)));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), tcpChecksum(src, dst, std.mem.asBytes(&tcp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Ipv6 header is wire-exact 40 bytes" {
|
||||||
|
try std.testing.expectEqual(@as(usize, 40), @sizeOf(Ipv6Hdr));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "tcpChecksum6 equals a full RFC 1071 sum over the assembled IPv6 pseudo-header (independent path)" {
|
||||||
|
const src = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||||
|
const dst = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
|
||||||
|
const seg = [_]u8{ 0xde, 0xad, 0x00, 0x50, 0x11, 0x22, 0x33, 0x44, 0, 0, 0, 0, 0x50, 0x02, 0x04, 0x00, 0, 0, 0, 0 };
|
||||||
|
var buf: [40 + seg.len]u8 = undefined;
|
||||||
|
@memcpy(buf[0..16], &src);
|
||||||
|
@memcpy(buf[16..32], &dst);
|
||||||
|
std.mem.writeInt(u32, buf[32..36], @intCast(seg.len), .big);
|
||||||
|
buf[36] = 0;
|
||||||
|
buf[37] = 0;
|
||||||
|
buf[38] = 0;
|
||||||
|
buf[39] = 6;
|
||||||
|
@memcpy(buf[40..], &seg);
|
||||||
|
try std.testing.expectEqual(checksum(&buf), tcpChecksum6(src, dst, &seg));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "tcpChecksum6 self-verifies: a correctly-summed segment folds back to 0" {
|
||||||
|
const src = [16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||||
|
const dst = [16]u8{ 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
|
||||||
|
var tcp = TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, 54321),
|
||||||
|
.dst_port = std.mem.nativeToBig(u16, 443),
|
||||||
|
.seq = std.mem.nativeToBig(u32, 0x1234_5678),
|
||||||
|
.ack = 0,
|
||||||
|
.data_off_ns = 0x50,
|
||||||
|
.flags = 0x02,
|
||||||
|
.window = std.mem.nativeToBig(u16, 65535),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
tcp.checksum = std.mem.nativeToBig(u16, tcpChecksum6(src, dst, std.mem.asBytes(&tcp)));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), tcpChecksum6(src, dst, std.mem.asBytes(&tcp)));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "udpChecksum self-verifies: a correct datagram re-sums to the 0xFFFF all-ones marker" {
|
||||||
|
const src = std.mem.nativeToBig(u32, 0x7f000001);
|
||||||
|
const dst = std.mem.nativeToBig(u32, 0x08080808);
|
||||||
|
var seg: [12]u8 = undefined;
|
||||||
|
var hdr = UdpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, 40000),
|
||||||
|
.dst_port = std.mem.nativeToBig(u16, 53),
|
||||||
|
.length = std.mem.nativeToBig(u16, 12),
|
||||||
|
.checksum = 0,
|
||||||
|
};
|
||||||
|
@memcpy(seg[0..8], std.mem.asBytes(&hdr));
|
||||||
|
@memcpy(seg[8..12], "abcd");
|
||||||
|
const ck = udpChecksum(src, dst, &seg);
|
||||||
|
try std.testing.expect(ck != 0);
|
||||||
|
hdr.checksum = std.mem.nativeToBig(u16, ck);
|
||||||
|
@memcpy(seg[0..8], std.mem.asBytes(&hdr));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0xffff), udpChecksum(src, dst, &seg));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "udpChecksum maps a computed 0x0000 to 0xFFFF (IPv4 UDP quirk)" {
|
||||||
|
try std.testing.expectEqual(@as(u16, 0xffff), udpChecksum(0, 0, &[_]u8{ 0xff, 0xec }));
|
||||||
|
try std.testing.expect(udpChecksum(0, 0, &[_]u8{ 0xff, 0xec }) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the Linux SYN option chain decodes to the authoritative JA4T kind list 2-4-8-1-3" {
|
||||||
|
var kinds: [16]u8 = undefined;
|
||||||
|
const n = optionKinds(OsProfile.linux.options(), &kinds);
|
||||||
|
try std.testing.expectEqualSlices(u8, &.{ 2, 4, 8, 1, 3 }, kinds[0..n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the Windows SYN option chain omits the timestamp (kinds 2-1-3-1-1-4)" {
|
||||||
|
var kinds: [16]u8 = undefined;
|
||||||
|
const n = optionKinds(OsProfile.windows.options(), &kinds);
|
||||||
|
try std.testing.expectEqualSlices(u8, &.{ 2, 1, 3, 1, 1, 4 }, kinds[0..n]);
|
||||||
|
for (kinds[0..n]) |k| try std.testing.expect(k != 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the macOS SYN option chain carries the timestamp before SACK (kinds 2-1-3-1-1-8-4)" {
|
||||||
|
var kinds: [16]u8 = undefined;
|
||||||
|
const n = optionKinds(OsProfile.macos.options(), &kinds);
|
||||||
|
try std.testing.expectEqualSlices(u8, &.{ 2, 1, 3, 1, 1, 8, 4 }, kinds[0..n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the masscan profile sends exactly one MSS option and the fingerprintable 1024 window" {
|
||||||
|
var kinds: [16]u8 = undefined;
|
||||||
|
const n = optionKinds(OsProfile.masscan.options(), &kinds);
|
||||||
|
try std.testing.expectEqualSlices(u8, &.{2}, kinds[0..n]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1024), OsProfile.masscan.window());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the bare profile sends no options" {
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), OsProfile.none.options().len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "every OS profile advertises the ethernet MSS 1460" {
|
||||||
|
for ([_]OsProfile{ .masscan, .linux, .windows, .macos }) |p| {
|
||||||
|
const opts = p.options();
|
||||||
|
try std.testing.expectEqual(opt_mss, opts[0]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1460), std.mem.readInt(u16, opts[2..4], .big));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the timestamp offset points at four zero bytes inside the option chain" {
|
||||||
|
inline for ([_]OsProfile{ .linux, .macos }) |p| {
|
||||||
|
const off = p.tsValOffset().?;
|
||||||
|
const opts = p.options();
|
||||||
|
try std.testing.expectEqual(opt_ts, opts[off - 2]);
|
||||||
|
try std.testing.expectEqual(opt_ts_len, opts[off - 1]);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0), std.mem.readInt(u32, opts[off..][0..4], .big));
|
||||||
|
}
|
||||||
|
try std.testing.expect(OsProfile.windows.tsValOffset() == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "scan-type probe flags match the RFC 793 flag combinations" {
|
||||||
|
try std.testing.expectEqual(TcpFlag.syn, ScanType.syn.probeFlags());
|
||||||
|
try std.testing.expectEqual(TcpFlag.fin, ScanType.fin.probeFlags());
|
||||||
|
try std.testing.expectEqual(@as(u8, 0), ScanType.null_scan.probeFlags());
|
||||||
|
try std.testing.expectEqual(TcpFlag.fin | TcpFlag.psh | TcpFlag.urg, ScanType.xmas.probeFlags());
|
||||||
|
try std.testing.expectEqual(TcpFlag.fin | TcpFlag.ack, ScanType.maimon.probeFlags());
|
||||||
|
try std.testing.expectEqual(TcpFlag.ack, ScanType.ack.probeFlags());
|
||||||
|
try std.testing.expectEqual(TcpFlag.ack, ScanType.window.probeFlags());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ack-flag scans carry the cookie in the ack field, seq-scans in the seq field" {
|
||||||
|
for ([_]ScanType{ .maimon, .ack, .window }) |st| try std.testing.expect(st.cookieInAck());
|
||||||
|
for ([_]ScanType{ .syn, .fin, .null_scan, .xmas }) |st| try std.testing.expect(!st.cookieInAck());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seqConsumed reflects whether the probe advances the sequence space" {
|
||||||
|
for ([_]ScanType{ .syn, .fin, .xmas }) |st| try std.testing.expectEqual(@as(u32, 1), st.seqConsumed());
|
||||||
|
for ([_]ScanType{ .null_scan, .maimon, .ack, .window }) |st| try std.testing.expectEqual(@as(u32, 0), st.seqConsumed());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "scan-type and OS-profile parsers round-trip the CLI spellings" {
|
||||||
|
try std.testing.expectEqual(ScanType.null_scan, ScanType.parse("null").?);
|
||||||
|
try std.testing.expectEqual(ScanType.window, ScanType.parse("window").?);
|
||||||
|
try std.testing.expect(ScanType.parse("bogus") == null);
|
||||||
|
try std.testing.expectEqual(OsProfile.macos, OsProfile.parse("macos").?);
|
||||||
|
try std.testing.expect(OsProfile.parse("bogus") == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Addr.eql distinguishes families and values" {
|
||||||
|
const a = Addr{ .v4 = 0x0a000001 };
|
||||||
|
const b = Addr{ .v4 = 0x0a000001 };
|
||||||
|
const c = Addr{ .v4 = 0x0a000002 };
|
||||||
|
const v6a = Addr{ .v6 = [_]u8{0} ** 15 ++ [_]u8{1} };
|
||||||
|
const v6b = Addr{ .v6 = [_]u8{0} ** 15 ++ [_]u8{1} };
|
||||||
|
const v6c = Addr{ .v6 = [_]u8{0} ** 15 ++ [_]u8{2} };
|
||||||
|
try std.testing.expect(a.eql(b));
|
||||||
|
try std.testing.expect(!a.eql(c));
|
||||||
|
try std.testing.expect(v6a.eql(v6b));
|
||||||
|
try std.testing.expect(!v6a.eql(v6c));
|
||||||
|
try std.testing.expect(!a.eql(v6a));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Addr.order sorts v4 before v6, then by value" {
|
||||||
|
const v4lo = Addr{ .v4 = 1 };
|
||||||
|
const v4hi = Addr{ .v4 = 2 };
|
||||||
|
const v6lo = Addr{ .v6 = [_]u8{0} ** 16 };
|
||||||
|
const v6hi = Addr{ .v6 = [_]u8{0} ** 15 ++ [_]u8{1} };
|
||||||
|
try std.testing.expectEqual(std.math.Order.lt, v4lo.order(v4hi));
|
||||||
|
try std.testing.expectEqual(std.math.Order.lt, v4hi.order(v6lo));
|
||||||
|
try std.testing.expectEqual(std.math.Order.lt, v6lo.order(v6hi));
|
||||||
|
try std.testing.expectEqual(std.math.Order.eq, v6hi.order(v6hi));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,112 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// packet_io.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const build_config = @import("build_config");
|
||||||
|
const afpacket = @import("afpacket");
|
||||||
|
const afxdp = @import("afxdp");
|
||||||
|
|
||||||
|
pub const Kind = enum { afpacket, afxdp_copy, afxdp_zerocopy };
|
||||||
|
pub const Choice = enum { auto, xdp, afpacket };
|
||||||
|
|
||||||
|
pub const Backend = union(enum) {
|
||||||
|
afpacket: afpacket.Backend,
|
||||||
|
afxdp: afxdp.Backend,
|
||||||
|
|
||||||
|
pub fn submit(self: *Backend, frame: []const u8) bool {
|
||||||
|
return switch (self.*) {
|
||||||
|
inline else => |*b| b.submit(frame),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kick(self: *Backend) void {
|
||||||
|
switch (self.*) {
|
||||||
|
inline else => |*b| b.kick(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *Backend) void {
|
||||||
|
switch (self.*) {
|
||||||
|
inline else => |*b| b.close(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kind(self: *const Backend) Kind {
|
||||||
|
return switch (self.*) {
|
||||||
|
.afpacket => .afpacket,
|
||||||
|
.afxdp => |*b| if (b.zerocopy()) .afxdp_zerocopy else .afxdp_copy,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const SelectError = error{XdpNotCompiledIn} || afxdp.OpenError || afpacket.OpenError;
|
||||||
|
|
||||||
|
pub fn parseChoice(text: ?[]const u8) ?Choice {
|
||||||
|
const t = text orelse return .auto;
|
||||||
|
if (std.mem.eql(u8, t, "auto")) return .auto;
|
||||||
|
if (std.mem.eql(u8, t, "xdp")) return .xdp;
|
||||||
|
if (std.mem.eql(u8, t, "afpacket")) return .afpacket;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kindLabel(k: Kind) []const u8 {
|
||||||
|
return switch (k) {
|
||||||
|
.afpacket => "AF_PACKET (PACKET_TX_RING)",
|
||||||
|
.afxdp_copy => "AF_XDP (copy / XDP_SKB mode)",
|
||||||
|
.afxdp_zerocopy => "AF_XDP (zero-copy)",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn note(diag: ?*std.Io.Writer, comptime fmt: []const u8, args: anytype) void {
|
||||||
|
if (diag) |w| {
|
||||||
|
w.print(" backend: " ++ fmt ++ "\n", args) catch {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select(
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
ifname: []const u8,
|
||||||
|
choice: Choice,
|
||||||
|
xdp_cfg: afxdp.Config,
|
||||||
|
afp_cfg: afpacket.RingConfig,
|
||||||
|
diag: ?*std.Io.Writer,
|
||||||
|
) SelectError!Backend {
|
||||||
|
if (choice == .xdp and !build_config.xdp) return error.XdpNotCompiledIn;
|
||||||
|
|
||||||
|
const want_xdp = build_config.xdp and choice != .afpacket;
|
||||||
|
if (want_xdp) {
|
||||||
|
if (afxdp.Backend.open(allocator, ifname, .zerocopy, xdp_cfg)) |b| {
|
||||||
|
return .{ .afxdp = b };
|
||||||
|
} else |err| {
|
||||||
|
note(diag, "AF_XDP zero-copy unavailable ({s})", .{@errorName(err)});
|
||||||
|
}
|
||||||
|
if (afxdp.Backend.open(allocator, ifname, .copy, xdp_cfg)) |b| {
|
||||||
|
return .{ .afxdp = b };
|
||||||
|
} else |err| {
|
||||||
|
if (choice == .xdp) return err;
|
||||||
|
note(diag, "AF_XDP copy mode unavailable ({s}); using AF_PACKET", .{@errorName(err)});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return .{ .afpacket = try afpacket.Backend.open(ifname, afp_cfg) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseChoice maps flag text, defaults to auto, and rejects unknown values" {
|
||||||
|
try std.testing.expectEqual(Choice.auto, parseChoice(null).?);
|
||||||
|
try std.testing.expectEqual(Choice.auto, parseChoice("auto").?);
|
||||||
|
try std.testing.expectEqual(Choice.xdp, parseChoice("xdp").?);
|
||||||
|
try std.testing.expectEqual(Choice.afpacket, parseChoice("afpacket").?);
|
||||||
|
try std.testing.expect(parseChoice("nonsense") == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "forcing --backend xdp without a -Dxdp build is rejected up front" {
|
||||||
|
if (!build_config.xdp) {
|
||||||
|
const r = select(std.testing.allocator, "lo", .xdp, .{}, .{}, null);
|
||||||
|
try std.testing.expectError(error.XdpNotCompiledIn, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "kindLabel covers every backend kind" {
|
||||||
|
try std.testing.expect(kindLabel(.afpacket).len > 0);
|
||||||
|
try std.testing.expect(kindLabel(.afxdp_copy).len > 0);
|
||||||
|
try std.testing.expect(kindLabel(.afxdp_zerocopy).len > 0);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,196 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// payloads.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const Entry = struct { port: u16, payload: []const u8 };
|
||||||
|
|
||||||
|
const ntp_client = [_]u8{0x1b} ++ [_]u8{0} ** 47;
|
||||||
|
|
||||||
|
const dns_version_bind = [_]u8{
|
||||||
|
0x13, 0x37,
|
||||||
|
0x01, 0x00,
|
||||||
|
0x00, 0x01,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x07, 'v',
|
||||||
|
'e', 'r',
|
||||||
|
's', 'i',
|
||||||
|
'o', 'n',
|
||||||
|
0x04, 'b',
|
||||||
|
'i', 'n',
|
||||||
|
'd', 0x00,
|
||||||
|
0x00, 0x10,
|
||||||
|
0x00, 0x03,
|
||||||
|
};
|
||||||
|
|
||||||
|
const snmp_get_public = [_]u8{
|
||||||
|
0x30, 0x26,
|
||||||
|
0x02, 0x01,
|
||||||
|
0x00, 0x04,
|
||||||
|
0x06, 'p',
|
||||||
|
'u', 'b',
|
||||||
|
'l', 'i',
|
||||||
|
'c', 0xa0,
|
||||||
|
0x19, 0x02,
|
||||||
|
0x01, 0x00,
|
||||||
|
0x02, 0x01,
|
||||||
|
0x00, 0x02,
|
||||||
|
0x01, 0x00,
|
||||||
|
0x30, 0x0e,
|
||||||
|
0x30, 0x0c,
|
||||||
|
0x06, 0x08,
|
||||||
|
0x2b, 0x06,
|
||||||
|
0x01, 0x02,
|
||||||
|
0x01, 0x01,
|
||||||
|
0x01, 0x00,
|
||||||
|
0x05, 0x00,
|
||||||
|
};
|
||||||
|
|
||||||
|
const netbios_node_status = [_]u8{
|
||||||
|
0x13, 0x37,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x01,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x20, 'C',
|
||||||
|
'K',
|
||||||
|
} ++ ([_]u8{'A'} ** 30) ++ [_]u8{
|
||||||
|
0x00,
|
||||||
|
0x00,
|
||||||
|
0x21,
|
||||||
|
0x00,
|
||||||
|
0x01,
|
||||||
|
};
|
||||||
|
|
||||||
|
const ssdp_msearch =
|
||||||
|
"M-SEARCH * HTTP/1.1\r\n" ++
|
||||||
|
"HOST:239.255.255.250:1900\r\n" ++
|
||||||
|
"MAN:\"ssdp:discover\"\r\n" ++
|
||||||
|
"MX:1\r\n" ++
|
||||||
|
"ST:ssdp:all\r\n" ++
|
||||||
|
"\r\n";
|
||||||
|
|
||||||
|
const mdns_services = [_]u8{
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x01,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x00, 0x00,
|
||||||
|
0x09, '_',
|
||||||
|
's', 'e',
|
||||||
|
'r', 'v',
|
||||||
|
'i', 'c',
|
||||||
|
'e', 's',
|
||||||
|
0x07, '_',
|
||||||
|
'd', 'n',
|
||||||
|
's', '-',
|
||||||
|
's', 'd',
|
||||||
|
0x04, '_',
|
||||||
|
'u', 'd',
|
||||||
|
'p', 0x05,
|
||||||
|
'l', 'o',
|
||||||
|
'c', 'a',
|
||||||
|
'l', 0x00,
|
||||||
|
0x00, 0x0c,
|
||||||
|
0x00, 0x01,
|
||||||
|
};
|
||||||
|
|
||||||
|
const table = [_]Entry{
|
||||||
|
.{ .port = 53, .payload = &dns_version_bind },
|
||||||
|
.{ .port = 123, .payload = &ntp_client },
|
||||||
|
.{ .port = 137, .payload = &netbios_node_status },
|
||||||
|
.{ .port = 161, .payload = &snmp_get_public },
|
||||||
|
.{ .port = 1900, .payload = ssdp_msearch },
|
||||||
|
.{ .port = 5353, .payload = &mdns_services },
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn lookup(port: u16) []const u8 {
|
||||||
|
inline for (table) |e| {
|
||||||
|
if (e.port == port) return e.payload;
|
||||||
|
}
|
||||||
|
return &.{};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const max_len: usize = blk: {
|
||||||
|
var m: usize = 0;
|
||||||
|
for (table) |e| {
|
||||||
|
if (e.payload.len > m) m = e.payload.len;
|
||||||
|
}
|
||||||
|
break :blk m;
|
||||||
|
};
|
||||||
|
|
||||||
|
test "lookup returns the protocol payload for a known port and empty for the rest" {
|
||||||
|
try std.testing.expectEqual(@as(usize, 48), lookup(123).len);
|
||||||
|
try std.testing.expectEqualSlices(u8, &ntp_client, lookup(123));
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), lookup(80).len);
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), lookup(0).len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "NTP client probe is a 48-byte LI=0 VN=3 Mode=3 request" {
|
||||||
|
const p = lookup(123);
|
||||||
|
try std.testing.expectEqual(@as(usize, 48), p.len);
|
||||||
|
try std.testing.expectEqual(@as(u8, 0x1b), p[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "DNS probe is a well-formed single-question version.bind CH TXT query" {
|
||||||
|
const p = lookup(53);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1), std.mem.readInt(u16, p[4..6], .big));
|
||||||
|
var name: [16]u8 = undefined;
|
||||||
|
var w: usize = 0;
|
||||||
|
var i: usize = 12;
|
||||||
|
while (p[i] != 0) {
|
||||||
|
const len = p[i];
|
||||||
|
i += 1;
|
||||||
|
if (w != 0) {
|
||||||
|
name[w] = '.';
|
||||||
|
w += 1;
|
||||||
|
}
|
||||||
|
@memcpy(name[w .. w + len], p[i .. i + len]);
|
||||||
|
w += len;
|
||||||
|
i += len;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqualStrings("version.bind", name[0..w]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x0010), std.mem.readInt(u16, p[i + 1 ..][0..2], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x0003), std.mem.readInt(u16, p[i + 3 ..][0..2], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SNMP probe is a v1 GetRequest for community public with a matching outer length" {
|
||||||
|
const p = lookup(161);
|
||||||
|
try std.testing.expectEqual(@as(u8, 0x30), p[0]);
|
||||||
|
try std.testing.expectEqual(@as(usize, p[1]) + 2, p.len);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, p, "public") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOfScalar(u8, p, 0xa0) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "NetBIOS probe carries the encoded wildcard name and NBSTAT type" {
|
||||||
|
const p = lookup(137);
|
||||||
|
try std.testing.expectEqual(@as(usize, 50), p.len);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, p, "CKAAAA") != null);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x0021), std.mem.readInt(u16, p[p.len - 4 ..][0..2], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SSDP probe is an M-SEARCH discover" {
|
||||||
|
const p = lookup(1900);
|
||||||
|
try std.testing.expect(std.mem.startsWith(u8, p, "M-SEARCH * HTTP/1.1"));
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, p, "ssdp:discover") != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "mDNS probe enumerates DNS-SD services over PTR" {
|
||||||
|
const p = lookup(5353);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, p, "_services") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, p, "_dns-sd") != null);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x000c), std.mem.readInt(u16, p[p.len - 4 ..][0..2], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "max_len equals the longest table payload" {
|
||||||
|
var m: usize = 0;
|
||||||
|
for (table) |e| {
|
||||||
|
if (e.payload.len > m) m = e.payload.len;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(m, max_len);
|
||||||
|
try std.testing.expect(max_len >= 48);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,202 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// probe.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const regex = @import("regex");
|
||||||
|
|
||||||
|
pub const max_info: usize = 96;
|
||||||
|
pub const max_banner: usize = 512;
|
||||||
|
|
||||||
|
pub const http_get = "GET / HTTP/1.0\r\n\r\n";
|
||||||
|
|
||||||
|
const http_ports = [_]u16{ 80, 81, 591, 3000, 5000, 8000, 8008, 8080, 8081, 8888, 9000 };
|
||||||
|
|
||||||
|
pub fn probeFor(port: u16) []const u8 {
|
||||||
|
for (http_ports) |p| {
|
||||||
|
if (p == port) return http_get;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const ServiceInfo = struct {
|
||||||
|
service: []const u8 = "unknown",
|
||||||
|
info: [max_info]u8 = [_]u8{0} ** max_info,
|
||||||
|
info_len: usize = 0,
|
||||||
|
tls: bool = false,
|
||||||
|
|
||||||
|
pub fn infoSlice(self: *const ServiceInfo) []const u8 {
|
||||||
|
return self.info[0..self.info_len];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const CompiledRule = struct {
|
||||||
|
prefix: []const u8,
|
||||||
|
service: []const u8,
|
||||||
|
re: ?regex.Regex,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn rule(comptime prefix: []const u8, comptime service: []const u8, comptime pattern: []const u8, comptime flags: regex.Regex.Flags) CompiledRule {
|
||||||
|
return .{
|
||||||
|
.prefix = prefix,
|
||||||
|
.service = service,
|
||||||
|
.re = if (pattern.len == 0) null else (regex.Regex.compile(pattern, flags) catch @compileError("probe: bad pattern " ++ pattern)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const rules = blk: {
|
||||||
|
@setEvalBranchQuota(400_000);
|
||||||
|
break :blk [_]CompiledRule{
|
||||||
|
rule("SSH-", "ssh", "^SSH-[\\d.]+-([^\\r\\n]+)", .{}),
|
||||||
|
rule("HTTP/", "http", "[Ss]erver:[ \\t]*([^\\r\\n]+)", .{}),
|
||||||
|
rule("+OK", "pop3", "^\\+OK ([^\\r\\n]+)", .{}),
|
||||||
|
rule("* OK", "imap", "^\\* OK ([^\\r\\n]+)", .{}),
|
||||||
|
rule("RFB ", "vnc", "^RFB (\\d+\\.\\d+)", .{}),
|
||||||
|
rule("\xff", "telnet", "", .{}),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
fn startsWith(bytes: []const u8, prefix: []const u8) bool {
|
||||||
|
return bytes.len >= prefix.len and std.mem.eql(u8, bytes[0..prefix.len], prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isTls(bytes: []const u8) bool {
|
||||||
|
if (bytes.len < 3) return false;
|
||||||
|
const content = bytes[0];
|
||||||
|
return (content == 0x16 or content == 0x15) and bytes[1] == 0x03 and bytes[2] <= 0x04;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sanitizeInto(out: *ServiceInfo, src: []const u8) void {
|
||||||
|
var n: usize = 0;
|
||||||
|
for (src) |c| {
|
||||||
|
if (n >= max_info) break;
|
||||||
|
out.info[n] = if (c >= 0x20 and c < 0x7f) c else '.';
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
out.info_len = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn firstLine(bytes: []const u8) []const u8 {
|
||||||
|
const cut = std.mem.indexOfAny(u8, bytes, "\r\n") orelse bytes.len;
|
||||||
|
return bytes[0..cut];
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extractInfo(out: *ServiceInfo, re: *const regex.Regex, bytes: []const u8) void {
|
||||||
|
var caps: regex.Captures = .{};
|
||||||
|
if (re.search(bytes, &caps) != null) {
|
||||||
|
if (caps.group(bytes, 1)) |g| sanitizeInto(out, g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve220(bytes: []const u8) []const u8 {
|
||||||
|
if (std.mem.indexOf(u8, bytes, "SMTP") != null or std.mem.indexOf(u8, bytes, "ESMTP") != null) return "smtp";
|
||||||
|
if (std.mem.indexOf(u8, bytes, "FTP") != null) return "ftp";
|
||||||
|
return "ftp";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classify(bytes: []const u8, out: *ServiceInfo) void {
|
||||||
|
out.* = .{};
|
||||||
|
if (bytes.len == 0) return;
|
||||||
|
|
||||||
|
if (isTls(bytes)) {
|
||||||
|
out.service = "ssl/tls";
|
||||||
|
out.tls = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startsWith(bytes, "220-") or startsWith(bytes, "220 ")) {
|
||||||
|
out.service = resolve220(bytes);
|
||||||
|
sanitizeInto(out, firstLine(bytes));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (rules) |r| {
|
||||||
|
if (!startsWith(bytes, r.prefix)) continue;
|
||||||
|
out.service = r.service;
|
||||||
|
if (r.re) |*re| extractInfo(out, re, bytes) else sanitizeInto(out, firstLine(bytes));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
out.service = "unknown";
|
||||||
|
sanitizeInto(out, firstLine(bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- tests ----
|
||||||
|
|
||||||
|
test "probeFor sends an HTTP GET on web ports and nothing elsewhere" {
|
||||||
|
try std.testing.expectEqualStrings(http_get, probeFor(80));
|
||||||
|
try std.testing.expectEqualStrings(http_get, probeFor(8080));
|
||||||
|
try std.testing.expectEqualStrings("", probeFor(22));
|
||||||
|
try std.testing.expectEqualStrings("", probeFor(443));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SSH banner classifies as ssh and extracts the software string" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("SSH-2.0-OpenSSH_9.6p1 Debian-3\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("ssh", si.service);
|
||||||
|
try std.testing.expectEqualStrings("OpenSSH_9.6p1 Debian-3", si.infoSlice());
|
||||||
|
try std.testing.expect(!si.tls);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "HTTP response classifies as http and pulls the Server header" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("HTTP/1.1 200 OK\r\nServer: nginx/1.24.0\r\nContent-Length: 5\r\n\r\nhello", &si);
|
||||||
|
try std.testing.expectEqualStrings("http", si.service);
|
||||||
|
try std.testing.expectEqualStrings("nginx/1.24.0", si.infoSlice());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "SMTP and FTP both greet with 220 but disambiguate by content" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("220 mail.example.com ESMTP Postfix\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("smtp", si.service);
|
||||||
|
classify("220 ProFTPD Server ready\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("ftp", si.service);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "POP3 and IMAP greetings classify by their prefixes" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("+OK Dovecot ready.\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("pop3", si.service);
|
||||||
|
try std.testing.expectEqualStrings("Dovecot ready.", si.infoSlice());
|
||||||
|
classify("* OK [CAPABILITY IMAP4rev1] Dovecot ready\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("imap", si.service);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a TLS record is detected without decrypting it" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify(&[_]u8{ 0x16, 0x03, 0x03, 0x00, 0x50, 0x02 }, &si);
|
||||||
|
try std.testing.expectEqualStrings("ssl/tls", si.service);
|
||||||
|
try std.testing.expect(si.tls);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an unrecognized banner is reported as unknown with its first line" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("GARBAGE PROTOCOL v3\r\nsecond line\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("unknown", si.service);
|
||||||
|
try std.testing.expectEqualStrings("GARBAGE PROTOCOL v3", si.infoSlice());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a hostile banner cannot inject terminal escapes into the info field" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("SSH-2.0-\x1b[31mEVIL\x1b[0m\x07\r\n", &si);
|
||||||
|
try std.testing.expectEqualStrings("ssh", si.service);
|
||||||
|
try std.testing.expect(std.mem.indexOfScalar(u8, si.infoSlice(), 0x1b) == null);
|
||||||
|
try std.testing.expect(std.mem.indexOfScalar(u8, si.infoSlice(), 0x07) == null);
|
||||||
|
try std.testing.expectEqualStrings(".[31mEVIL.[0m.", si.infoSlice());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the info field is length-capped even for a very long banner" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
var big: [400]u8 = undefined;
|
||||||
|
@memcpy(big[0..4], "+OK ");
|
||||||
|
@memset(big[4..], 'A');
|
||||||
|
classify(&big, &si);
|
||||||
|
try std.testing.expectEqualStrings("pop3", si.service);
|
||||||
|
try std.testing.expect(si.info_len <= max_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "empty input yields the inert default" {
|
||||||
|
var si: ServiceInfo = .{};
|
||||||
|
classify("", &si);
|
||||||
|
try std.testing.expectEqualStrings("unknown", si.service);
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), si.info_len);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,150 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// ratelimit.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const NS_PER_SEC: u64 = 1_000_000_000;
|
||||||
|
const max_gap_ns: f64 = 3.6e12;
|
||||||
|
|
||||||
|
pub const Jitter = struct {
|
||||||
|
prng: std.Random.DefaultPrng,
|
||||||
|
mean_ns: f64,
|
||||||
|
|
||||||
|
pub fn init(seed: u64, mean_ns: u64) Jitter {
|
||||||
|
return .{ .prng = std.Random.DefaultPrng.init(seed), .mean_ns = @floatFromInt(mean_ns) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn nextGapNs(self: *Jitter) u64 {
|
||||||
|
const u = self.prng.random().float(f64);
|
||||||
|
const safe_u = if (u <= 0.0) std.math.floatMin(f64) else u;
|
||||||
|
const gap = -@log(safe_u) * self.mean_ns;
|
||||||
|
return @intFromFloat(std.math.clamp(gap, 0.0, max_gap_ns));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const TokenBucket = struct {
|
||||||
|
step_ns: u64,
|
||||||
|
cap_ns: u64,
|
||||||
|
bank_ns: u64,
|
||||||
|
last_ns: u64,
|
||||||
|
jitter: ?Jitter = null,
|
||||||
|
|
||||||
|
pub fn init(rate_pps: u64, capacity: u64) TokenBucket {
|
||||||
|
const step = if (rate_pps == 0) NS_PER_SEC else NS_PER_SEC / rate_pps;
|
||||||
|
const safe_step = if (step == 0) 1 else step;
|
||||||
|
return .{
|
||||||
|
.step_ns = safe_step,
|
||||||
|
.cap_ns = safe_step * capacity,
|
||||||
|
.bank_ns = 0,
|
||||||
|
.last_ns = 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn withJitter(self: TokenBucket, seed: u64) TokenBucket {
|
||||||
|
var b = self;
|
||||||
|
b.jitter = Jitter.init(seed, self.step_ns);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prime(self: *TokenBucket, now_ns: u64) void {
|
||||||
|
self.last_ns = now_ns;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refund(self: *TokenBucket, tokens: u64) void {
|
||||||
|
self.bank_ns = @min(self.bank_ns +| tokens *| self.step_ns, self.cap_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn takeBatch(self: *TokenBucket, now_ns: u64, want: u64) u64 {
|
||||||
|
if (now_ns > self.last_ns) {
|
||||||
|
const elapsed = now_ns - self.last_ns;
|
||||||
|
self.bank_ns = @min(self.bank_ns +| elapsed, self.cap_ns);
|
||||||
|
}
|
||||||
|
self.last_ns = now_ns;
|
||||||
|
const available = self.bank_ns / self.step_ns;
|
||||||
|
const granted = @min(want, available);
|
||||||
|
self.bank_ns -= granted * self.step_ns;
|
||||||
|
return granted;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "bucket starts empty and grants one token per step_ns" {
|
||||||
|
var tb = TokenBucket.init(1000, 10);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(0, 5));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), tb.takeBatch(1_000_000, 5));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(1_500_000, 5));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), tb.takeBatch(2_000_000, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "burst is capped at capacity" {
|
||||||
|
var tb = TokenBucket.init(1000, 10);
|
||||||
|
try std.testing.expectEqual(@as(u64, 10), tb.takeBatch(1_000_000_000, 1000));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(1_000_000_000, 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "takeBatch grants only up to want" {
|
||||||
|
var tb = TokenBucket.init(1_000_000, 100);
|
||||||
|
try std.testing.expectEqual(@as(u64, 50), tb.takeBatch(100_000, 50));
|
||||||
|
try std.testing.expectEqual(@as(u64, 50), tb.takeBatch(100_000, 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "non-monotonic now does not over-credit" {
|
||||||
|
var tb = TokenBucket.init(1000, 10);
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), tb.takeBatch(1_000_000, 5));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(500_000, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "zero rate degrades to one-token-per-second, never divides by zero" {
|
||||||
|
var tb = TokenBucket.init(0, 4);
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), tb.takeBatch(NS_PER_SEC, 10));
|
||||||
|
try std.testing.expectEqual(@as(u64, 4), tb.takeBatch(NS_PER_SEC * 10, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "cold prime starts the bank empty so a low-rate scan does not front-load a burst" {
|
||||||
|
var tb = TokenBucket.init(1000, 64);
|
||||||
|
tb.prime(5_000_000_000);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(5_000_000_000, 100));
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), tb.takeBatch(5_001_000_000, 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "refund returns unused tokens to the bank, clamped at capacity" {
|
||||||
|
var tb = TokenBucket.init(1000, 10);
|
||||||
|
try std.testing.expectEqual(@as(u64, 10), tb.takeBatch(1_000_000_000, 10));
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), tb.takeBatch(1_000_000_000, 10));
|
||||||
|
tb.refund(4);
|
||||||
|
try std.testing.expectEqual(@as(u64, 4), tb.takeBatch(1_000_000_000, 10));
|
||||||
|
tb.refund(1000);
|
||||||
|
try std.testing.expectEqual(@as(u64, 10), tb.takeBatch(1_000_000_000, 100));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "withJitter attaches a Poisson pacer keyed to the configured step" {
|
||||||
|
const tb = TokenBucket.init(1000, 1000).withJitter(0xABCDEF);
|
||||||
|
try std.testing.expect(tb.jitter != null);
|
||||||
|
try std.testing.expectEqual(@as(f64, 1_000_000.0), tb.jitter.?.mean_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Poisson gaps average to the mean and are not constant" {
|
||||||
|
var jit = Jitter.init(0x5EED_1234, 1_000_000);
|
||||||
|
const n: usize = 40_000;
|
||||||
|
var total: u128 = 0;
|
||||||
|
const first: u64 = jit.nextGapNs();
|
||||||
|
var saw_different = false;
|
||||||
|
total += first;
|
||||||
|
var i: usize = 1;
|
||||||
|
while (i < n) : (i += 1) {
|
||||||
|
const g = jit.nextGapNs();
|
||||||
|
total += g;
|
||||||
|
if (g != first) saw_different = true;
|
||||||
|
}
|
||||||
|
const mean = @as(f64, @floatFromInt(@as(u64, @intCast(total / n))));
|
||||||
|
try std.testing.expect(saw_different);
|
||||||
|
try std.testing.expect(mean > 900_000.0 and mean < 1_100_000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Poisson gap survives the u=0 edge without dividing into infinity" {
|
||||||
|
var jit = Jitter.init(1, 500);
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < 1000) : (i += 1) {
|
||||||
|
const g = jit.nextGapNs();
|
||||||
|
try std.testing.expect(g <= @as(u64, @intFromFloat(max_gap_ns)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,172 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// rawprobe.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
pub const Status = enum {
|
||||||
|
ok,
|
||||||
|
no_cap,
|
||||||
|
no_egress,
|
||||||
|
|
||||||
|
pub fn reason(self: Status) []const u8 {
|
||||||
|
return switch (self) {
|
||||||
|
.ok => "raw sends work",
|
||||||
|
.no_cap => "no CAP_NET_RAW",
|
||||||
|
.no_egress => "raw sends are silently dropped",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const ETH_P_ALL: u16 = 0x0003;
|
||||||
|
const probe_ethertype: u16 = 0x88b5;
|
||||||
|
const eth_hdr_len: usize = 14;
|
||||||
|
const nonce_off: usize = eth_hdr_len;
|
||||||
|
const nonce_len: usize = 8;
|
||||||
|
const frame_len: usize = 60;
|
||||||
|
const poll_budget_ms: i64 = 250;
|
||||||
|
const poll_tick_ms: i32 = 25;
|
||||||
|
const probe_mac = [6]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
fn monoMs() i64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(i64, @intCast(ts.sec)) * 1000 + @divTrunc(@as(i64, @intCast(ts.nsec)), 1_000_000);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn freshNonce() [nonce_len]u8 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
const mixed: u64 = (@as(u64, @intCast(ts.nsec)) ^ (@as(u64, @intCast(ts.sec)) << 20)) *% 0x9e3779b97f4a7c15;
|
||||||
|
var out: [nonce_len]u8 = undefined;
|
||||||
|
std.mem.writeInt(u64, &out, mixed, .little);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn buildFrame(nonce: [nonce_len]u8) [frame_len]u8 {
|
||||||
|
var frame = [_]u8{0} ** frame_len;
|
||||||
|
@memcpy(frame[0..6], &probe_mac);
|
||||||
|
@memcpy(frame[6..12], &probe_mac);
|
||||||
|
std.mem.writeInt(u16, frame[12..14], probe_ethertype, .big);
|
||||||
|
@memcpy(frame[nonce_off .. nonce_off + nonce_len], &nonce);
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn carriesNonce(frame: []const u8, nonce: [nonce_len]u8) bool {
|
||||||
|
if (frame.len < nonce_off + nonce_len) return false;
|
||||||
|
if (std.mem.readInt(u16, frame[12..14], .big) != probe_ethertype) return false;
|
||||||
|
return std.mem.eql(u8, frame[nonce_off .. nonce_off + nonce_len], &nonce);
|
||||||
|
}
|
||||||
|
|
||||||
|
const send_bursts: usize = 3;
|
||||||
|
|
||||||
|
const BoundSock = struct { fd: i32, sll: linux.sockaddr.ll };
|
||||||
|
|
||||||
|
const OpenError = error{ NoCap, Failed };
|
||||||
|
|
||||||
|
fn openBound(ifname: []const u8) OpenError!BoundSock {
|
||||||
|
const rc_sock = linux.socket(linux.AF.PACKET, linux.SOCK.RAW, std.mem.nativeToBig(u16, ETH_P_ALL));
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NoCap,
|
||||||
|
else => return error.Failed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.Failed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS) return error.Failed;
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, ETH_P_ALL);
|
||||||
|
sll.ifindex = ifr.ifru.ivalue;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS) return error.Failed;
|
||||||
|
return .{ .fd = fd, .sll = sll };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn openStatus(e: OpenError) Status {
|
||||||
|
return switch (e) {
|
||||||
|
error.NoCap => .no_cap,
|
||||||
|
error.Failed => .no_egress,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn probe(ifname: []const u8) Status {
|
||||||
|
const observer = openBound(ifname) catch |e| return openStatus(e);
|
||||||
|
defer _ = linux.close(observer.fd);
|
||||||
|
var sender = openBound(ifname) catch |e| return openStatus(e);
|
||||||
|
defer _ = linux.close(sender.fd);
|
||||||
|
|
||||||
|
const nonce = freshNonce();
|
||||||
|
const frame = buildFrame(nonce);
|
||||||
|
var burst: usize = 0;
|
||||||
|
while (burst < send_bursts) : (burst += 1) {
|
||||||
|
_ = linux.sendto(sender.fd, &frame, frame.len, 0, @ptrCast(&sender.sll), @sizeOf(linux.sockaddr.ll));
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf: [2048]u8 = undefined;
|
||||||
|
const deadline = monoMs() + poll_budget_ms;
|
||||||
|
while (monoMs() < deadline) {
|
||||||
|
var pfd = [_]linux.pollfd{.{ .fd = observer.fd, .events = linux.POLL.IN, .revents = 0 }};
|
||||||
|
const pr = linux.poll(&pfd, 1, poll_tick_ms);
|
||||||
|
switch (linux.errno(pr)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return .no_egress,
|
||||||
|
}
|
||||||
|
if (pr == 0) continue;
|
||||||
|
const rc = linux.recvfrom(observer.fd, &buf, buf.len, 0, null, null);
|
||||||
|
switch (linux.errno(rc)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR, .AGAIN => continue,
|
||||||
|
else => return .no_egress,
|
||||||
|
}
|
||||||
|
const n: usize = @intCast(rc);
|
||||||
|
if (carriesNonce(buf[0..n], nonce)) return .ok;
|
||||||
|
}
|
||||||
|
return .no_egress;
|
||||||
|
}
|
||||||
|
|
||||||
|
test "buildFrame stamps the experimental ethertype, self-addressed MACs, and the nonce" {
|
||||||
|
const nonce = [8]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
|
||||||
|
const f = buildFrame(nonce);
|
||||||
|
try std.testing.expectEqual(@as(usize, 60), f.len);
|
||||||
|
try std.testing.expectEqualSlices(u8, &probe_mac, f[0..6]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &probe_mac, f[6..12]);
|
||||||
|
try std.testing.expectEqual(@as(u16, probe_ethertype), std.mem.readInt(u16, f[12..14], .big));
|
||||||
|
try std.testing.expectEqualSlices(u8, &nonce, f[14..22]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "carriesNonce matches only our tagged frame, rejects other traffic and wrong nonce" {
|
||||||
|
const nonce = freshNonce();
|
||||||
|
const other = freshNonce();
|
||||||
|
const f = buildFrame(nonce);
|
||||||
|
try std.testing.expect(carriesNonce(&f, nonce));
|
||||||
|
try std.testing.expect(!carriesNonce(&f, other));
|
||||||
|
|
||||||
|
var ip_frame = [_]u8{0} ** 60;
|
||||||
|
std.mem.writeInt(u16, ip_frame[12..14], 0x0800, .big);
|
||||||
|
@memcpy(ip_frame[14..22], &nonce);
|
||||||
|
try std.testing.expect(!carriesNonce(&ip_frame, nonce));
|
||||||
|
|
||||||
|
try std.testing.expect(!carriesNonce(&[_]u8{ 0, 1, 2 }, nonce));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "freshNonce is not trivially constant across calls" {
|
||||||
|
const a = freshNonce();
|
||||||
|
var differs = false;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < 8) : (i += 1) {
|
||||||
|
const b = freshNonce();
|
||||||
|
if (!std.mem.eql(u8, &a, &b)) differs = true;
|
||||||
|
}
|
||||||
|
try std.testing.expect(differs);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "probe returns no_cap without CAP_NET_RAW, otherwise a defined status" {
|
||||||
|
const st = probe("lo");
|
||||||
|
try std.testing.expect(st == .ok or st == .no_cap or st == .no_egress);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,695 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// regex.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const max_insts: usize = 160;
|
||||||
|
pub const max_nodes: usize = 160;
|
||||||
|
pub const max_classes: usize = 16;
|
||||||
|
pub const max_caps: usize = 8;
|
||||||
|
pub const max_repeat: usize = 512;
|
||||||
|
pub const default_budget: u32 = 200_000;
|
||||||
|
pub const max_depth: usize = 8192;
|
||||||
|
|
||||||
|
const cap_slots: usize = (max_caps + 1) * 2;
|
||||||
|
|
||||||
|
pub const CompileError = error{
|
||||||
|
ProgramTooBig,
|
||||||
|
TooManyNodes,
|
||||||
|
TooManyClasses,
|
||||||
|
TooManyCaptures,
|
||||||
|
UnbalancedParen,
|
||||||
|
UnbalancedClass,
|
||||||
|
BadQuantifier,
|
||||||
|
QuantifierOnGroup,
|
||||||
|
TrailingBackslash,
|
||||||
|
EmptyRepeat,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Op = enum { char, any, class, match, jmp, split, save, bol, eol };
|
||||||
|
|
||||||
|
const Inst = struct {
|
||||||
|
op: Op,
|
||||||
|
a: u32 = 0,
|
||||||
|
b: u32 = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Class = struct {
|
||||||
|
neg: bool = false,
|
||||||
|
bits: [32]u8 = [_]u8{0} ** 32,
|
||||||
|
|
||||||
|
fn set(self: *Class, byte: u8) void {
|
||||||
|
self.bits[byte >> 3] |= (@as(u8, 1) << @intCast(byte & 7));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setRange(self: *Class, lo: u8, hi: u8) void {
|
||||||
|
var b: usize = lo;
|
||||||
|
while (b <= hi) : (b += 1) self.set(@intCast(b));
|
||||||
|
if (hi == 255) self.set(255);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has(self: *const Class, byte: u8) bool {
|
||||||
|
const in = (self.bits[byte >> 3] & (@as(u8, 1) << @intCast(byte & 7))) != 0;
|
||||||
|
return in != self.neg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const NodeKind = enum { empty, char, any, class, bol, eol, concat, alt, star, plus, quest, repeat, group };
|
||||||
|
|
||||||
|
const Node = struct {
|
||||||
|
kind: NodeKind,
|
||||||
|
ch: u8 = 0,
|
||||||
|
class_idx: u16 = 0,
|
||||||
|
left: u16 = 0,
|
||||||
|
right: u16 = 0,
|
||||||
|
greedy: bool = true,
|
||||||
|
min: u16 = 0,
|
||||||
|
max: u16 = 0,
|
||||||
|
cap: i16 = -1,
|
||||||
|
};
|
||||||
|
|
||||||
|
const unbounded: u16 = 0xffff;
|
||||||
|
|
||||||
|
pub const Captures = struct {
|
||||||
|
slots: [cap_slots]u32 = [_]u32{unset} ** cap_slots,
|
||||||
|
|
||||||
|
const unset: u32 = 0xffff_ffff;
|
||||||
|
|
||||||
|
pub fn group(self: *const Captures, input: []const u8, idx: usize) ?[]const u8 {
|
||||||
|
const s = idx * 2;
|
||||||
|
if (s + 1 >= cap_slots) return null;
|
||||||
|
const a = self.slots[s];
|
||||||
|
const b = self.slots[s + 1];
|
||||||
|
if (a == unset or b == unset or b < a or b > input.len) return null;
|
||||||
|
return input[a..b];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Regex = struct {
|
||||||
|
insts: [max_insts]Inst = [_]Inst{.{ .op = .match }} ** max_insts,
|
||||||
|
n_insts: usize = 0,
|
||||||
|
classes: [max_classes]Class = [_]Class{.{}} ** max_classes,
|
||||||
|
n_classes: usize = 0,
|
||||||
|
n_caps: usize = 0,
|
||||||
|
fold: bool = false,
|
||||||
|
dotall: bool = false,
|
||||||
|
|
||||||
|
pub const Flags = struct {
|
||||||
|
fold: bool = false,
|
||||||
|
dotall: bool = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn compile(pattern: []const u8, flags: Flags) CompileError!Regex {
|
||||||
|
var b = Builder{
|
||||||
|
.pat = pattern,
|
||||||
|
.fold = flags.fold,
|
||||||
|
};
|
||||||
|
var re: Regex = .{ .fold = flags.fold, .dotall = flags.dotall };
|
||||||
|
const root = try b.parseAlt(&re);
|
||||||
|
if (b.pos != pattern.len) return error.UnbalancedParen;
|
||||||
|
try b.emit(&re, root);
|
||||||
|
try re.push(.{ .op = .match });
|
||||||
|
re.n_caps = b.n_caps;
|
||||||
|
return re;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn push(self: *Regex, inst: Inst) CompileError!void {
|
||||||
|
if (self.n_insts >= max_insts) return error.ProgramTooBig;
|
||||||
|
self.insts[self.n_insts] = inst;
|
||||||
|
self.n_insts += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn addClass(self: *Regex, cls: Class) CompileError!u16 {
|
||||||
|
if (self.n_classes >= max_classes) return error.TooManyClasses;
|
||||||
|
const idx = self.n_classes;
|
||||||
|
self.classes[idx] = cls;
|
||||||
|
self.n_classes += 1;
|
||||||
|
return @intCast(idx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foldByte(self: *const Regex, c: u8) u8 {
|
||||||
|
return if (self.fold) std.ascii.toLower(c) else c;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn step(self: *const Regex, input: []const u8, pc0: usize, sp0: usize, caps: *Captures, budget: *u32, depth: usize) ?usize {
|
||||||
|
if (depth > max_depth) return null;
|
||||||
|
var pc = pc0;
|
||||||
|
var sp = sp0;
|
||||||
|
while (true) {
|
||||||
|
if (budget.* == 0) return null;
|
||||||
|
budget.* -= 1;
|
||||||
|
const in = self.insts[pc];
|
||||||
|
switch (in.op) {
|
||||||
|
.char => {
|
||||||
|
if (sp < input.len and self.foldByte(input[sp]) == @as(u8, @intCast(in.a))) {
|
||||||
|
pc += 1;
|
||||||
|
sp += 1;
|
||||||
|
} else return null;
|
||||||
|
},
|
||||||
|
.any => {
|
||||||
|
if (sp < input.len and (self.dotall or input[sp] != '\n')) {
|
||||||
|
pc += 1;
|
||||||
|
sp += 1;
|
||||||
|
} else return null;
|
||||||
|
},
|
||||||
|
.class => {
|
||||||
|
if (sp < input.len and self.classes[in.a].has(input[sp])) {
|
||||||
|
pc += 1;
|
||||||
|
sp += 1;
|
||||||
|
} else return null;
|
||||||
|
},
|
||||||
|
.bol => {
|
||||||
|
if (sp == 0) {
|
||||||
|
pc += 1;
|
||||||
|
} else return null;
|
||||||
|
},
|
||||||
|
.eol => {
|
||||||
|
if (sp == input.len) {
|
||||||
|
pc += 1;
|
||||||
|
} else return null;
|
||||||
|
},
|
||||||
|
.save => {
|
||||||
|
if (in.a < cap_slots) caps.slots[in.a] = @intCast(sp);
|
||||||
|
pc += 1;
|
||||||
|
},
|
||||||
|
.jmp => pc = in.a,
|
||||||
|
.split => {
|
||||||
|
const snap = caps.*;
|
||||||
|
if (self.step(input, in.a, sp, caps, budget, depth + 1)) |e| return e;
|
||||||
|
caps.* = snap;
|
||||||
|
pc = in.b;
|
||||||
|
},
|
||||||
|
.match => return sp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn search(self: *const Regex, input: []const u8, caps: *Captures) ?usize {
|
||||||
|
var budget: u32 = default_budget;
|
||||||
|
var start: usize = 0;
|
||||||
|
while (start <= input.len) : (start += 1) {
|
||||||
|
caps.* = .{};
|
||||||
|
if (self.step(input, 0, start, caps, &budget, 0)) |end| {
|
||||||
|
caps.slots[0] = @intCast(start);
|
||||||
|
caps.slots[1] = @intCast(end);
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
if (budget == 0) return null;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Builder = struct {
|
||||||
|
pat: []const u8,
|
||||||
|
pos: usize = 0,
|
||||||
|
fold: bool,
|
||||||
|
n_nodes: u16 = 0,
|
||||||
|
nodes: [max_nodes]Node = undefined,
|
||||||
|
n_caps: usize = 0,
|
||||||
|
|
||||||
|
fn peek(self: *const Builder) ?u8 {
|
||||||
|
return if (self.pos < self.pat.len) self.pat[self.pos] else null;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add(self: *Builder, node: Node) CompileError!u16 {
|
||||||
|
if (self.n_nodes >= max_nodes) return error.TooManyNodes;
|
||||||
|
const idx = self.n_nodes;
|
||||||
|
self.nodes[idx] = node;
|
||||||
|
self.n_nodes += 1;
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseAlt(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
var left = try self.parseConcat(re);
|
||||||
|
while (self.peek() == @as(u8, '|')) {
|
||||||
|
self.pos += 1;
|
||||||
|
const right = try self.parseConcat(re);
|
||||||
|
left = try self.add(.{ .kind = .alt, .left = left, .right = right });
|
||||||
|
}
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseConcat(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
var acc: ?u16 = null;
|
||||||
|
while (self.peek()) |c| {
|
||||||
|
if (c == '|' or c == ')') break;
|
||||||
|
const piece = try self.parsePiece(re);
|
||||||
|
acc = if (acc) |a| try self.add(.{ .kind = .concat, .left = a, .right = piece }) else piece;
|
||||||
|
}
|
||||||
|
return acc orelse try self.add(.{ .kind = .empty });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parsePiece(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
const atom = try self.parseAtom(re);
|
||||||
|
const c = self.peek() orelse return atom;
|
||||||
|
switch (c) {
|
||||||
|
'*', '+', '?' => {
|
||||||
|
self.pos += 1;
|
||||||
|
const greedy = !(self.peek() == @as(u8, '?'));
|
||||||
|
if (!greedy) self.pos += 1;
|
||||||
|
const kind: NodeKind = switch (c) {
|
||||||
|
'*' => .star,
|
||||||
|
'+' => .plus,
|
||||||
|
else => .quest,
|
||||||
|
};
|
||||||
|
return self.add(.{ .kind = kind, .left = atom, .greedy = greedy });
|
||||||
|
},
|
||||||
|
'{' => return self.parseRepeat(atom),
|
||||||
|
else => return atom,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseRepeat(self: *Builder, atom: u16) CompileError!u16 {
|
||||||
|
if (self.nodes[atom].kind == .group or self.nodes[atom].kind == .alt or self.nodes[atom].kind == .concat)
|
||||||
|
return error.QuantifierOnGroup;
|
||||||
|
self.pos += 1;
|
||||||
|
const min = try self.readNumber();
|
||||||
|
var max: u16 = min;
|
||||||
|
if (self.peek() == @as(u8, ',')) {
|
||||||
|
self.pos += 1;
|
||||||
|
if (self.peek() == @as(u8, '}')) {
|
||||||
|
max = unbounded;
|
||||||
|
} else {
|
||||||
|
max = try self.readNumber();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (self.peek() != @as(u8, '}')) return error.BadQuantifier;
|
||||||
|
self.pos += 1;
|
||||||
|
if (max != unbounded and max < min) return error.BadQuantifier;
|
||||||
|
if (max != unbounded and max > max_repeat) return error.BadQuantifier;
|
||||||
|
const greedy = !(self.peek() == @as(u8, '?'));
|
||||||
|
if (!greedy) self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .repeat, .left = atom, .min = min, .max = max, .greedy = greedy });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn readNumber(self: *Builder) CompileError!u16 {
|
||||||
|
var n: u32 = 0;
|
||||||
|
var seen = false;
|
||||||
|
while (self.peek()) |c| {
|
||||||
|
if (c < '0' or c > '9') break;
|
||||||
|
n = n * 10 + (c - '0');
|
||||||
|
if (n > max_repeat) n = max_repeat + 1;
|
||||||
|
self.pos += 1;
|
||||||
|
seen = true;
|
||||||
|
}
|
||||||
|
if (!seen) return error.BadQuantifier;
|
||||||
|
return @intCast(@min(n, @as(u32, unbounded)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseAtom(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
const c = self.peek() orelse return self.add(.{ .kind = .empty });
|
||||||
|
switch (c) {
|
||||||
|
'(' => {
|
||||||
|
self.pos += 1;
|
||||||
|
var cap: i16 = -1;
|
||||||
|
if (self.pos + 1 < self.pat.len and self.pat[self.pos] == '?' and self.pat[self.pos + 1] == ':') {
|
||||||
|
self.pos += 2;
|
||||||
|
} else {
|
||||||
|
self.n_caps += 1;
|
||||||
|
if (self.n_caps > max_caps) return error.TooManyCaptures;
|
||||||
|
cap = @intCast(self.n_caps);
|
||||||
|
}
|
||||||
|
const inner = try self.parseAlt(re);
|
||||||
|
if (self.peek() != @as(u8, ')')) return error.UnbalancedParen;
|
||||||
|
self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .group, .left = inner, .cap = cap });
|
||||||
|
},
|
||||||
|
'[' => return self.parseClass(re),
|
||||||
|
'.' => {
|
||||||
|
self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .any });
|
||||||
|
},
|
||||||
|
'^' => {
|
||||||
|
self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .bol });
|
||||||
|
},
|
||||||
|
'$' => {
|
||||||
|
self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .eol });
|
||||||
|
},
|
||||||
|
'\\' => return self.parseEscape(re),
|
||||||
|
else => {
|
||||||
|
self.pos += 1;
|
||||||
|
return self.add(.{ .kind = .char, .ch = c });
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseEscape(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
self.pos += 1;
|
||||||
|
const c = self.peek() orelse return error.TrailingBackslash;
|
||||||
|
self.pos += 1;
|
||||||
|
switch (c) {
|
||||||
|
'd', 'D', 'w', 'W', 's', 'S' => {
|
||||||
|
const ci = try self.addShorthand(re, c);
|
||||||
|
return self.add(.{ .kind = .class, .class_idx = ci });
|
||||||
|
},
|
||||||
|
'n' => return self.add(.{ .kind = .char, .ch = '\n' }),
|
||||||
|
'r' => return self.add(.{ .kind = .char, .ch = '\r' }),
|
||||||
|
't' => return self.add(.{ .kind = .char, .ch = '\t' }),
|
||||||
|
'0' => return self.add(.{ .kind = .char, .ch = 0 }),
|
||||||
|
else => return self.add(.{ .kind = .char, .ch = c }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn addShorthand(self: *Builder, re: *Regex, c: u8) CompileError!u16 {
|
||||||
|
_ = self;
|
||||||
|
var cls = Class{};
|
||||||
|
fillShorthand(&cls, c);
|
||||||
|
return re.addClass(cls);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseClass(self: *Builder, re: *Regex) CompileError!u16 {
|
||||||
|
self.pos += 1;
|
||||||
|
var cls = Class{};
|
||||||
|
if (self.peek() == @as(u8, '^')) {
|
||||||
|
cls.neg = true;
|
||||||
|
self.pos += 1;
|
||||||
|
}
|
||||||
|
var first = true;
|
||||||
|
while (true) {
|
||||||
|
const c = self.peek() orelse return error.UnbalancedClass;
|
||||||
|
if (c == ']' and !first) {
|
||||||
|
self.pos += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
if (c == '\\') {
|
||||||
|
self.pos += 1;
|
||||||
|
const e = self.peek() orelse return error.TrailingBackslash;
|
||||||
|
self.pos += 1;
|
||||||
|
switch (e) {
|
||||||
|
'd', 'D', 'w', 'W', 's', 'S' => fillShorthand(&cls, e),
|
||||||
|
'n' => self.classSet(&cls, '\n'),
|
||||||
|
'r' => self.classSet(&cls, '\r'),
|
||||||
|
't' => self.classSet(&cls, '\t'),
|
||||||
|
else => self.classSet(&cls, e),
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
self.pos += 1;
|
||||||
|
if (self.peek() == @as(u8, '-') and self.pos + 1 < self.pat.len and self.pat[self.pos + 1] != ']') {
|
||||||
|
self.pos += 1;
|
||||||
|
const hi = self.peek().?;
|
||||||
|
self.pos += 1;
|
||||||
|
const lo_c = c;
|
||||||
|
if (hi < lo_c) return error.UnbalancedClass;
|
||||||
|
cls.setRange(lo_c, hi);
|
||||||
|
if (self.fold) {
|
||||||
|
self.foldRange(&cls, lo_c, hi);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.classSet(&cls, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const ci = try re.addClass(cls);
|
||||||
|
return self.add(.{ .kind = .class, .class_idx = ci });
|
||||||
|
}
|
||||||
|
|
||||||
|
fn classSet(self: *Builder, cls: *Class, c: u8) void {
|
||||||
|
cls.set(c);
|
||||||
|
if (self.fold) {
|
||||||
|
cls.set(std.ascii.toLower(c));
|
||||||
|
cls.set(std.ascii.toUpper(c));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foldRange(self: *Builder, cls: *Class, lo: u8, hi: u8) void {
|
||||||
|
_ = self;
|
||||||
|
var b: usize = lo;
|
||||||
|
while (b <= hi) : (b += 1) {
|
||||||
|
const ch: u8 = @intCast(b);
|
||||||
|
cls.set(std.ascii.toLower(ch));
|
||||||
|
cls.set(std.ascii.toUpper(ch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emit(self: *Builder, re: *Regex, node_idx: u16) CompileError!void {
|
||||||
|
const node = self.nodes[node_idx];
|
||||||
|
switch (node.kind) {
|
||||||
|
.empty => {},
|
||||||
|
.char => {
|
||||||
|
const ch = if (re.fold) std.ascii.toLower(node.ch) else node.ch;
|
||||||
|
try re.push(.{ .op = .char, .a = ch });
|
||||||
|
},
|
||||||
|
.any => try re.push(.{ .op = .any }),
|
||||||
|
.class => try re.push(.{ .op = .class, .a = node.class_idx }),
|
||||||
|
.bol => try re.push(.{ .op = .bol }),
|
||||||
|
.eol => try re.push(.{ .op = .eol }),
|
||||||
|
.concat => {
|
||||||
|
try self.emit(re, node.left);
|
||||||
|
try self.emit(re, node.right);
|
||||||
|
},
|
||||||
|
.alt => {
|
||||||
|
const isplit = re.n_insts;
|
||||||
|
try re.push(.{ .op = .split });
|
||||||
|
const l1 = re.n_insts;
|
||||||
|
try self.emit(re, node.left);
|
||||||
|
const ijmp = re.n_insts;
|
||||||
|
try re.push(.{ .op = .jmp });
|
||||||
|
const l2 = re.n_insts;
|
||||||
|
try self.emit(re, node.right);
|
||||||
|
const end = re.n_insts;
|
||||||
|
re.insts[isplit].a = @intCast(l1);
|
||||||
|
re.insts[isplit].b = @intCast(l2);
|
||||||
|
re.insts[ijmp].a = @intCast(end);
|
||||||
|
},
|
||||||
|
.group => {
|
||||||
|
if (node.cap >= 0) {
|
||||||
|
const c: usize = @intCast(node.cap);
|
||||||
|
try re.push(.{ .op = .save, .a = @intCast(c * 2) });
|
||||||
|
try self.emit(re, node.left);
|
||||||
|
try re.push(.{ .op = .save, .a = @intCast(c * 2 + 1) });
|
||||||
|
} else {
|
||||||
|
try self.emit(re, node.left);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.quest => try self.emitQuest(re, node.left, node.greedy),
|
||||||
|
.star => try self.emitStar(re, node.left, node.greedy),
|
||||||
|
.plus => try self.emitPlus(re, node.left, node.greedy),
|
||||||
|
.repeat => try self.emitRepeat(re, node.left, node.min, node.max, node.greedy),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emitQuest(self: *Builder, re: *Regex, child: u16, greedy: bool) CompileError!void {
|
||||||
|
const isplit = re.n_insts;
|
||||||
|
try re.push(.{ .op = .split });
|
||||||
|
const l1 = re.n_insts;
|
||||||
|
try self.emit(re, child);
|
||||||
|
const end = re.n_insts;
|
||||||
|
setSplit(re, isplit, l1, end, greedy);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emitStar(self: *Builder, re: *Regex, child: u16, greedy: bool) CompileError!void {
|
||||||
|
const isplit = re.n_insts;
|
||||||
|
try re.push(.{ .op = .split });
|
||||||
|
const l1 = re.n_insts;
|
||||||
|
try self.emit(re, child);
|
||||||
|
try re.push(.{ .op = .jmp, .a = @intCast(isplit) });
|
||||||
|
const end = re.n_insts;
|
||||||
|
setSplit(re, isplit, l1, end, greedy);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emitPlus(self: *Builder, re: *Regex, child: u16, greedy: bool) CompileError!void {
|
||||||
|
const l1 = re.n_insts;
|
||||||
|
try self.emit(re, child);
|
||||||
|
const isplit = re.n_insts;
|
||||||
|
try re.push(.{ .op = .split });
|
||||||
|
const end = re.n_insts;
|
||||||
|
setSplit(re, isplit, l1, end, greedy);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emitRepeat(self: *Builder, re: *Regex, child: u16, min: u16, max: u16, greedy: bool) CompileError!void {
|
||||||
|
var k: usize = 0;
|
||||||
|
while (k < min) : (k += 1) try self.emit(re, child);
|
||||||
|
if (max == unbounded) {
|
||||||
|
try self.emitStar(re, child, greedy);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const opt = max - min;
|
||||||
|
var splits: [max_repeat]usize = undefined;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (i < opt) : (i += 1) {
|
||||||
|
splits[i] = re.n_insts;
|
||||||
|
try re.push(.{ .op = .split });
|
||||||
|
try self.emit(re, child);
|
||||||
|
}
|
||||||
|
const end = re.n_insts;
|
||||||
|
i = 0;
|
||||||
|
while (i < opt) : (i += 1) setSplit(re, splits[i], splits[i] + 1, end, greedy);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn setSplit(re: *Regex, idx: usize, body: usize, exit: usize, greedy: bool) void {
|
||||||
|
if (greedy) {
|
||||||
|
re.insts[idx].a = @intCast(body);
|
||||||
|
re.insts[idx].b = @intCast(exit);
|
||||||
|
} else {
|
||||||
|
re.insts[idx].a = @intCast(exit);
|
||||||
|
re.insts[idx].b = @intCast(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isDigit(b: u8) bool {
|
||||||
|
return b >= '0' and b <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isWord(b: u8) bool {
|
||||||
|
return (b >= '0' and b <= '9') or (b >= 'a' and b <= 'z') or (b >= 'A' and b <= 'Z') or b == '_';
|
||||||
|
}
|
||||||
|
|
||||||
|
fn isSpace(b: u8) bool {
|
||||||
|
return b == ' ' or b == '\t' or b == '\n' or b == '\r' or b == 0x0c or b == 0x0b;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fillShorthand(cls: *Class, c: u8) void {
|
||||||
|
var b: usize = 0;
|
||||||
|
while (b <= 255) : (b += 1) {
|
||||||
|
const ch: u8 = @intCast(b);
|
||||||
|
const in = switch (c) {
|
||||||
|
'd' => isDigit(ch),
|
||||||
|
'D' => !isDigit(ch),
|
||||||
|
'w' => isWord(ch),
|
||||||
|
'W' => !isWord(ch),
|
||||||
|
's' => isSpace(ch),
|
||||||
|
'S' => !isSpace(ch),
|
||||||
|
else => false,
|
||||||
|
};
|
||||||
|
if (in) cls.set(ch);
|
||||||
|
if (b == 255) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- tests ----
|
||||||
|
|
||||||
|
fn expectMatch(pattern: []const u8, input: []const u8) !void {
|
||||||
|
const re = try Regex.compile(pattern, .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
try std.testing.expect(re.search(input, &caps) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expectNoMatch(pattern: []const u8, input: []const u8) !void {
|
||||||
|
const re = try Regex.compile(pattern, .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
try std.testing.expect(re.search(input, &caps) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "literal, dot, and anchors" {
|
||||||
|
try expectMatch("abc", "xxabcyy");
|
||||||
|
try expectNoMatch("abc", "abx");
|
||||||
|
try expectMatch("a.c", "azc");
|
||||||
|
try expectNoMatch("a.c", "a\nc");
|
||||||
|
try expectMatch("^abc", "abcdef");
|
||||||
|
try expectNoMatch("^abc", "zabc");
|
||||||
|
try expectMatch("abc$", "zzabc");
|
||||||
|
try expectNoMatch("abc$", "abcz");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "star, plus, quest greedy" {
|
||||||
|
try expectMatch("ab*c", "ac");
|
||||||
|
try expectMatch("ab*c", "abbbbc");
|
||||||
|
try expectMatch("ab+c", "abc");
|
||||||
|
try expectNoMatch("ab+c", "ac");
|
||||||
|
try expectMatch("ab?c", "ac");
|
||||||
|
try expectMatch("ab?c", "abc");
|
||||||
|
try expectNoMatch("ab?c", "abbc");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "character classes with ranges, negation, and shorthands" {
|
||||||
|
try expectMatch("[a-f]+", "0deadbeef1");
|
||||||
|
try expectMatch("[^0-9]", "a");
|
||||||
|
try expectNoMatch("^[^0-9]$", "5");
|
||||||
|
try expectMatch("\\d+", "port 8080 open");
|
||||||
|
try expectMatch("^\\w+$", "host_name99");
|
||||||
|
try expectNoMatch("^\\w+$", "has space");
|
||||||
|
try expectMatch("a\\s+b", "a \t b");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "alternation and grouping" {
|
||||||
|
try expectMatch("cat|dog|bird", "i have a dog");
|
||||||
|
try expectNoMatch("^(cat|dog)$", "fish");
|
||||||
|
try expectMatch("^(ab)+$", "ababab");
|
||||||
|
try expectNoMatch("^(ab)+$", "aba");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "bounded repeat {n}, {n,}, {n,m}" {
|
||||||
|
try expectMatch("^a{3}$", "aaa");
|
||||||
|
try expectNoMatch("^a{3}$", "aa");
|
||||||
|
try expectMatch("^a{2,}$", "aaaaa");
|
||||||
|
try expectNoMatch("^a{2,}$", "a");
|
||||||
|
try expectMatch("^a{2,4}$", "aaa");
|
||||||
|
try expectNoMatch("^a{2,4}$", "aaaaa");
|
||||||
|
try expectMatch("^\\d{1,3}\\.\\d{1,3}$", "10.255");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "captures extract submatches" {
|
||||||
|
const re = try Regex.compile("^SSH-([\\d.]+)-(\\S+)", .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
const banner = "SSH-2.0-OpenSSH_9.6p1\r\n";
|
||||||
|
try std.testing.expect(re.search(banner, &caps) != null);
|
||||||
|
try std.testing.expectEqualStrings("2.0", caps.group(banner, 1).?);
|
||||||
|
try std.testing.expectEqualStrings("OpenSSH_9.6p1", caps.group(banner, 2).?);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "case-insensitive flag folds literals and ranges" {
|
||||||
|
const re = try Regex.compile("server: ([a-z]+)", .{ .fold = true });
|
||||||
|
var caps: Captures = .{};
|
||||||
|
const hdr = "Server: Nginx";
|
||||||
|
try std.testing.expect(re.search(hdr, &caps) != null);
|
||||||
|
try std.testing.expectEqualStrings("Nginx", caps.group(hdr, 1).?);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "dotall flag lets dot cross newlines" {
|
||||||
|
var caps: Captures = .{};
|
||||||
|
const plain = try Regex.compile("a.b", .{});
|
||||||
|
try std.testing.expect(plain.search("a\nb", &caps) == null);
|
||||||
|
const dotall = try Regex.compile("a.b", .{ .dotall = true });
|
||||||
|
try std.testing.expect(dotall.search("a\nb", &caps) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "lazy quantifier stops at the first opportunity" {
|
||||||
|
const re = try Regex.compile("<(.+?)>", .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
const s = "<a><b>";
|
||||||
|
try std.testing.expect(re.search(s, &caps) != null);
|
||||||
|
try std.testing.expectEqualStrings("a", caps.group(s, 1).?);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the step budget makes a pathological input fail closed instead of hanging" {
|
||||||
|
const re = try Regex.compile("(a+)+$", .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
const evil = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaX";
|
||||||
|
try std.testing.expect(re.search(evil, &caps) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the recursion-depth cap fails closed on a match deeper than the stack allows" {
|
||||||
|
var input: [max_depth + 100]u8 = undefined;
|
||||||
|
@memset(&input, 'a');
|
||||||
|
const re = try Regex.compile("^a*$", .{});
|
||||||
|
var caps: Captures = .{};
|
||||||
|
try std.testing.expect(re.search(&input, &caps) == null);
|
||||||
|
try std.testing.expect(re.search("aaaa", &caps) != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "escaped metacharacters are literal" {
|
||||||
|
try expectMatch("^\\d+\\.\\d+$", "3.14");
|
||||||
|
try expectNoMatch("^\\d+\\.\\d+$", "3x14");
|
||||||
|
try expectMatch("a\\+b", "a+b");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "empty pattern matches, unbalanced paren rejected" {
|
||||||
|
try expectMatch("", "anything");
|
||||||
|
try std.testing.expectError(error.UnbalancedParen, Regex.compile("(ab", .{}));
|
||||||
|
try std.testing.expectError(error.QuantifierOnGroup, Regex.compile("(ab){2}", .{}));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "compiles at comptime into an embeddable program" {
|
||||||
|
const re = comptime blk: {
|
||||||
|
@setEvalBranchQuota(20000);
|
||||||
|
break :blk Regex.compile("^220[- ]", .{}) catch unreachable;
|
||||||
|
};
|
||||||
|
var caps: Captures = .{};
|
||||||
|
try std.testing.expect(re.search("220 smtp.example.com ESMTP", &caps) != null);
|
||||||
|
try std.testing.expect(re.search("500 error", &caps) == null);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,315 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// rx.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const classify = @import("classify");
|
||||||
|
const dedup = @import("dedup");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
|
||||||
|
pub const Result = classify.Result;
|
||||||
|
pub const State = classify.State;
|
||||||
|
pub const TcpClassifier = classify.TcpClassifier;
|
||||||
|
pub const TcpClassifier6 = classify.TcpClassifier6;
|
||||||
|
pub const UdpClassifier = classify.UdpClassifier;
|
||||||
|
|
||||||
|
const RECV_BUF_LEN: usize = 2048;
|
||||||
|
const PORT_BITS: u6 = 16;
|
||||||
|
const NS_PER_MS: u64 = 1_000_000;
|
||||||
|
const NS_PER_SEC: u64 = 1_000_000_000;
|
||||||
|
|
||||||
|
pub fn resultKey(r: Result) u64 {
|
||||||
|
return switch (r.addr) {
|
||||||
|
.v4 => |ip| (@as(u64, ip) << PORT_BITS) | r.port,
|
||||||
|
.v6 => |b| blk: {
|
||||||
|
var h = std.hash.Wyhash.init(r.port);
|
||||||
|
h.update(&b);
|
||||||
|
const k = h.final();
|
||||||
|
break :blk if (k == std.math.maxInt(u64)) k -% 1 else k;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(source: anytype, clf: anytype, dd: *dedup.Dedup, sink: anytype) void {
|
||||||
|
var buf: [RECV_BUF_LEN]u8 = undefined;
|
||||||
|
while (source.recv(&buf)) |n| {
|
||||||
|
if (clf.match(buf[0..n])) |r| {
|
||||||
|
if (dd.insert(resultKey(r))) sink.emit(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const QueueSink = struct {
|
||||||
|
queue: *std.Io.Queue(Result),
|
||||||
|
io: std.Io,
|
||||||
|
|
||||||
|
pub fn emit(self: *QueueSink, r: Result) void {
|
||||||
|
self.queue.putOne(self.io, r) catch {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
pub const ETH_P_IP: u16 = 0x0800;
|
||||||
|
pub const ETH_P_IPV6: u16 = 0x86dd;
|
||||||
|
|
||||||
|
pub const OpenError = error{
|
||||||
|
NeedCapNetRaw,
|
||||||
|
SocketFailed,
|
||||||
|
IfIndexFailed,
|
||||||
|
BindFailed,
|
||||||
|
};
|
||||||
|
|
||||||
|
const POLL_TICK_MS: i32 = 100;
|
||||||
|
|
||||||
|
pub const RecvPlan = union(enum) { stop, poll: i32 };
|
||||||
|
|
||||||
|
pub fn planDrain(
|
||||||
|
now_ns: u64,
|
||||||
|
hard_deadline_ns: u64,
|
||||||
|
tx_done: bool,
|
||||||
|
drain_anchor_ns: *?u64,
|
||||||
|
drain_window_ns: u64,
|
||||||
|
tick_ms: i32,
|
||||||
|
) RecvPlan {
|
||||||
|
if (now_ns >= hard_deadline_ns) return .stop;
|
||||||
|
if (!tx_done) return .{ .poll = tick_ms };
|
||||||
|
if (drain_anchor_ns.* == null) drain_anchor_ns.* = now_ns;
|
||||||
|
const deadline = drain_anchor_ns.*.? + drain_window_ns;
|
||||||
|
if (now_ns >= deadline) return .stop;
|
||||||
|
const remaining_ms: u64 = (deadline - now_ns) / NS_PER_MS;
|
||||||
|
const cap: u64 = @intCast(tick_ms);
|
||||||
|
const chosen: u64 = @min(remaining_ms + 1, cap);
|
||||||
|
return .{ .poll = @intCast(chosen) };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Receiver = struct {
|
||||||
|
fd: i32,
|
||||||
|
tx_done: *std.atomic.Value(bool),
|
||||||
|
drain_window_ns: u64,
|
||||||
|
hard_cap_ns: u64,
|
||||||
|
started: bool = false,
|
||||||
|
hard_deadline_ns: u64 = 0,
|
||||||
|
drain_anchor_ns: ?u64 = null,
|
||||||
|
|
||||||
|
fn monoNow() u64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(u64, @intCast(ts.sec)) * NS_PER_SEC + @as(u64, @intCast(ts.nsec));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open(ifname: []const u8, proto: u16, tx_done: *std.atomic.Value(bool), drain_window_ns: u64, hard_cap_ns: u64) OpenError!Receiver {
|
||||||
|
const rc_sock = linux.socket(
|
||||||
|
linux.AF.PACKET,
|
||||||
|
linux.SOCK.RAW,
|
||||||
|
std.mem.nativeToBig(u16, proto),
|
||||||
|
);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.SocketFailed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.IfIndexFailed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS)
|
||||||
|
return error.IfIndexFailed;
|
||||||
|
const ifindex: i32 = ifr.ifru.ivalue;
|
||||||
|
|
||||||
|
var ignore_outgoing: u32 = 1;
|
||||||
|
_ = linux.setsockopt(fd, linux.SOL.PACKET, linux.PACKET.IGNORE_OUTGOING, std.mem.asBytes(&ignore_outgoing), @sizeOf(u32));
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, proto);
|
||||||
|
sll.ifindex = ifindex;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS)
|
||||||
|
return error.BindFailed;
|
||||||
|
|
||||||
|
return .{ .fd = fd, .tx_done = tx_done, .drain_window_ns = drain_window_ns, .hard_cap_ns = hard_cap_ns };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn recv(self: *Receiver, buf: []u8) ?usize {
|
||||||
|
while (true) {
|
||||||
|
const now = monoNow();
|
||||||
|
if (!self.started) {
|
||||||
|
self.started = true;
|
||||||
|
self.hard_deadline_ns = now +| self.hard_cap_ns;
|
||||||
|
}
|
||||||
|
const done = self.tx_done.load(.acquire);
|
||||||
|
const timeout: i32 = switch (planDrain(now, self.hard_deadline_ns, done, &self.drain_anchor_ns, self.drain_window_ns, POLL_TICK_MS)) {
|
||||||
|
.stop => return null,
|
||||||
|
.poll => |t| t,
|
||||||
|
};
|
||||||
|
var pfd = [_]linux.pollfd{.{ .fd = self.fd, .events = linux.POLL.IN, .revents = 0 }};
|
||||||
|
const pr = linux.poll(&pfd, 1, timeout);
|
||||||
|
switch (linux.errno(pr)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.INTR => continue,
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
if (pr == 0) continue;
|
||||||
|
const rc = linux.recvfrom(self.fd, buf.ptr, buf.len, 0, null, null);
|
||||||
|
switch (linux.errno(rc)) {
|
||||||
|
.SUCCESS => return @intCast(rc),
|
||||||
|
.INTR, .AGAIN => continue,
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *Receiver) void {
|
||||||
|
_ = linux.close(self.fd);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const our_ip: u32 = 0x0a000001;
|
||||||
|
const our_port: u16 = 40000;
|
||||||
|
const target_ip: u32 = 0x08080808;
|
||||||
|
|
||||||
|
fn buildReply(buf: *[54]u8, sport: u16, ack: u32, flags: u8) void {
|
||||||
|
@memset(buf, 0);
|
||||||
|
std.mem.writeInt(u16, buf[12..14], 0x0800, .big);
|
||||||
|
buf[14] = 0x45;
|
||||||
|
buf[14 + 9] = 6;
|
||||||
|
std.mem.writeInt(u32, buf[14 + 12 ..][0..4], target_ip, .big);
|
||||||
|
std.mem.writeInt(u32, buf[14 + 16 ..][0..4], our_ip, .big);
|
||||||
|
std.mem.writeInt(u16, buf[34..36], sport, .big);
|
||||||
|
std.mem.writeInt(u16, buf[36..38], our_port, .big);
|
||||||
|
std.mem.writeInt(u32, buf[42..46], ack, .big);
|
||||||
|
buf[47] = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FakeSource = struct {
|
||||||
|
frames: []const []const u8,
|
||||||
|
idx: usize = 0,
|
||||||
|
fn recv(self: *FakeSource, buf: []u8) ?usize {
|
||||||
|
if (self.idx >= self.frames.len) return null;
|
||||||
|
const f = self.frames[self.idx];
|
||||||
|
self.idx += 1;
|
||||||
|
@memcpy(buf[0..f.len], f);
|
||||||
|
return f.len;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const CollectSink = struct {
|
||||||
|
list: *std.ArrayList(Result),
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
fn emit(self: *CollectSink, r: Result) void {
|
||||||
|
self.list.append(self.allocator, r) catch {};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "engine classifies, dedups, and emits each found host once" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const seq80 = ck.seq(target_ip, 80, our_ip, our_port);
|
||||||
|
const seq81 = ck.seq(target_ip, 81, our_ip, our_port);
|
||||||
|
|
||||||
|
var open_a: [54]u8 = undefined;
|
||||||
|
var open_b: [54]u8 = undefined;
|
||||||
|
var closed_c: [54]u8 = undefined;
|
||||||
|
buildReply(&open_a, 80, seq80 +% 1, 0x12);
|
||||||
|
buildReply(&open_b, 80, seq80 +% 1, 0x12);
|
||||||
|
buildReply(&closed_c, 81, seq81 +% 1, 0x14);
|
||||||
|
const frames = [_][]const u8{ &open_a, &open_b, &closed_c };
|
||||||
|
|
||||||
|
var dd = try dedup.Dedup.init(std.testing.allocator, 16);
|
||||||
|
defer dd.deinit();
|
||||||
|
var list: std.ArrayList(Result) = .empty;
|
||||||
|
defer list.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
var src = FakeSource{ .frames = &frames };
|
||||||
|
var sink = CollectSink{ .list = &list, .allocator = std.testing.allocator };
|
||||||
|
run(&src, classify.TcpClassifier{ .ck = ck }, &dd, &sink);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), list.items.len);
|
||||||
|
try std.testing.expectEqual(State.open, list.items[0].state);
|
||||||
|
try std.testing.expectEqual(@as(u16, 80), list.items[0].port);
|
||||||
|
try std.testing.expectEqual(State.closed, list.items[1].state);
|
||||||
|
try std.testing.expectEqual(@as(u16, 81), list.items[1].port);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drainConsumer(io: std.Io, queue: *std.Io.Queue(Result), out: *std.ArrayList(Result), allocator: std.mem.Allocator) void {
|
||||||
|
while (true) {
|
||||||
|
const r = queue.getOne(io) catch break;
|
||||||
|
out.append(allocator, r) catch {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Io.Queue hands the deduped set from a producer to a consumer fiber" {
|
||||||
|
var threaded = std.Io.Threaded.init(std.testing.allocator, .{});
|
||||||
|
defer threaded.deinit();
|
||||||
|
const io = threaded.io();
|
||||||
|
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const seq80 = ck.seq(target_ip, 80, our_ip, our_port);
|
||||||
|
const seq81 = ck.seq(target_ip, 81, our_ip, our_port);
|
||||||
|
|
||||||
|
var open_a: [54]u8 = undefined;
|
||||||
|
var open_b: [54]u8 = undefined;
|
||||||
|
var closed_c: [54]u8 = undefined;
|
||||||
|
buildReply(&open_a, 80, seq80 +% 1, 0x12);
|
||||||
|
buildReply(&open_b, 80, seq80 +% 1, 0x12);
|
||||||
|
buildReply(&closed_c, 81, seq81 +% 1, 0x14);
|
||||||
|
const frames = [_][]const u8{ &open_a, &open_b, &closed_c };
|
||||||
|
|
||||||
|
var dd = try dedup.Dedup.init(std.testing.allocator, 16);
|
||||||
|
defer dd.deinit();
|
||||||
|
var out: std.ArrayList(Result) = .empty;
|
||||||
|
defer out.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
var qbuf: [8]Result = undefined;
|
||||||
|
var queue = std.Io.Queue(Result).init(&qbuf);
|
||||||
|
|
||||||
|
var consumer = io.async(drainConsumer, .{ io, &queue, &out, std.testing.allocator });
|
||||||
|
|
||||||
|
var src = FakeSource{ .frames = &frames };
|
||||||
|
var sink = QueueSink{ .queue = &queue, .io = io };
|
||||||
|
run(&src, classify.TcpClassifier{ .ck = ck }, &dd, &sink);
|
||||||
|
queue.close(io);
|
||||||
|
consumer.await(io);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), out.items.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ms: u64 = 1_000_000;
|
||||||
|
|
||||||
|
test "planDrain keeps polling while TX is still in flight (no quiet-gap early exit)" {
|
||||||
|
var anchor: ?u64 = null;
|
||||||
|
const p = planDrain(5 * ms, 100_000 * ms, false, &anchor, 2_000 * ms, POLL_TICK_MS);
|
||||||
|
switch (p) {
|
||||||
|
.poll => |t| try std.testing.expectEqual(POLL_TICK_MS, t),
|
||||||
|
.stop => return error.ShouldNotStopDuringTx,
|
||||||
|
}
|
||||||
|
try std.testing.expect(anchor == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "planDrain anchors the drain window at TX completion, not socket-open" {
|
||||||
|
var anchor: ?u64 = null;
|
||||||
|
const now: u64 = 30_000 * ms;
|
||||||
|
const p = planDrain(now, 100_000 * ms, true, &anchor, 2_000 * ms, POLL_TICK_MS);
|
||||||
|
try std.testing.expectEqual(@as(?u64, now), anchor);
|
||||||
|
switch (p) {
|
||||||
|
.poll => |t| try std.testing.expect(t >= 1 and t <= POLL_TICK_MS),
|
||||||
|
.stop => return error.ShouldStillDrain,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "planDrain stops once the post-TX drain window elapses" {
|
||||||
|
var anchor: ?u64 = 30_000 * ms;
|
||||||
|
const p = planDrain(32_001 * ms, 100_000 * ms, true, &anchor, 2_000 * ms, POLL_TICK_MS);
|
||||||
|
try std.testing.expect(std.meta.activeTag(p) == .stop);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "planDrain honors the hard safety cap even if TX never signalled done" {
|
||||||
|
var anchor: ?u64 = null;
|
||||||
|
const p = planDrain(9_999, 9_999, false, &anchor, 1, POLL_TICK_MS);
|
||||||
|
try std.testing.expect(std.meta.activeTag(p) == .stop);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,939 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// scancmd.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const targets = @import("targets");
|
||||||
|
const template = @import("template");
|
||||||
|
const udp = @import("udp");
|
||||||
|
const ratelimit = @import("ratelimit");
|
||||||
|
const packet_io = @import("packet_io");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const tx = @import("tx");
|
||||||
|
const rx = @import("rx");
|
||||||
|
const dedup = @import("dedup");
|
||||||
|
const netutil = @import("netutil");
|
||||||
|
const output = @import("output");
|
||||||
|
const stealth = @import("stealth");
|
||||||
|
const service = @import("service");
|
||||||
|
const connect = @import("connect");
|
||||||
|
const rawprobe = @import("rawprobe");
|
||||||
|
const ndp = @import("ndp");
|
||||||
|
|
||||||
|
const default_iface = "lo";
|
||||||
|
const default_rate: u64 = 10_000;
|
||||||
|
const default_src_port: u16 = 40_000;
|
||||||
|
const default_udp_src_span: u16 = 8_192;
|
||||||
|
const default_wait_ms: i32 = 2_000;
|
||||||
|
const ns_per_ms: u64 = 1_000_000;
|
||||||
|
const ns_per_sec: u64 = 1_000_000_000;
|
||||||
|
const default_tcp_ports = [_]u16{80};
|
||||||
|
const default_udp_ports = [_]u16{ 53, 123, 161 };
|
||||||
|
const dedup_capacity: usize = 1024;
|
||||||
|
const queue_capacity: usize = 2048;
|
||||||
|
const drain_batch: usize = 256;
|
||||||
|
const finding_queue_capacity: usize = 256;
|
||||||
|
const finding_drain_batch: usize = 64;
|
||||||
|
const drain_tick_ns: u64 = 50 * ns_per_ms;
|
||||||
|
const render_tick_interactive_ns: u64 = 125 * ns_per_ms;
|
||||||
|
const render_tick_plain_ns: u64 = 1_000 * ns_per_ms;
|
||||||
|
const rx_hard_cap_floor_ns: u64 = 60 * ns_per_sec;
|
||||||
|
const min_dashboard_cols: u16 = 64;
|
||||||
|
|
||||||
|
const need_cap_hint =
|
||||||
|
"scan: need CAP_NET_RAW + CAP_NET_ADMIN. Grant once, then re-run (no sudo):\n" ++
|
||||||
|
" sudo setcap cap_net_raw,cap_net_admin=eip ./zig-out/bin/zingela\nSkipping.\n";
|
||||||
|
|
||||||
|
const concurrency_hint =
|
||||||
|
"scan: this system cannot launch concurrent TX/RX (needs >= 2 worker threads).\n";
|
||||||
|
|
||||||
|
const authorized_warning =
|
||||||
|
"scan: stealth/evasion features require explicit authorization.\n" ++
|
||||||
|
"Re-run with --authorized-scan ONLY against systems you own or are\n" ++
|
||||||
|
"contractually authorized to test. Unauthorized scanning is a crime\n" ++
|
||||||
|
"under the CFAA and equivalent statutes worldwide.\n\n" ++
|
||||||
|
" stealth flags: --os-template --scan-type --jitter --source-port-rotation --decoys --suppress-rst\n\n" ++
|
||||||
|
stealth.omitted_help ++ "\n";
|
||||||
|
|
||||||
|
const TxSink = struct {
|
||||||
|
backend: *packet_io.Backend,
|
||||||
|
sent: *output.Counter,
|
||||||
|
|
||||||
|
pub fn submit(self: *TxSink, frame: []const u8) bool {
|
||||||
|
if (self.backend.submit(frame)) {
|
||||||
|
_ = self.sent.fetchAdd(1, .monotonic);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kick(self: *TxSink) void {
|
||||||
|
self.backend.kick();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const FindingSink = struct {
|
||||||
|
queue: *std.Io.Queue(service.Finding),
|
||||||
|
io: std.Io,
|
||||||
|
|
||||||
|
fn emitImpl(ctx: *anyopaque, f: service.Finding) void {
|
||||||
|
const self: *FindingSink = @ptrCast(@alignCast(ctx));
|
||||||
|
self.queue.putOne(self.io, f) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const vtable = service.Sink.Vtable{ .emit = emitImpl };
|
||||||
|
|
||||||
|
pub fn sink(self: *FindingSink) service.Sink {
|
||||||
|
return .{ .ctx = self, .vtable = &vtable };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn txWorkerImpl(
|
||||||
|
engine: *targets.Engine,
|
||||||
|
tmpl: anytype,
|
||||||
|
bucket: *ratelimit.TokenBucket,
|
||||||
|
sink: *TxSink,
|
||||||
|
max_packets: u64,
|
||||||
|
budget_ns: u64,
|
||||||
|
tx_done: *std.atomic.Value(bool),
|
||||||
|
) u64 {
|
||||||
|
var clock = netutil.RealClock{};
|
||||||
|
const deadline_ns = clock.now() +| budget_ns;
|
||||||
|
const sent = tx.run(engine, tmpl, bucket, sink, &clock, max_packets, deadline_ns);
|
||||||
|
tx_done.store(true, .release);
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn txWorkerTcp(engine: *targets.Engine, tmpl: *const template.SynTemplate, bucket: *ratelimit.TokenBucket, sink: *TxSink, max_packets: u64, budget_ns: u64, tx_done: *std.atomic.Value(bool)) u64 {
|
||||||
|
return txWorkerImpl(engine, tmpl, bucket, sink, max_packets, budget_ns, tx_done);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn txWorkerUdp(engine: *targets.Engine, tmpl: *const udp.UdpTemplate, bucket: *ratelimit.TokenBucket, sink: *TxSink, max_packets: u64, budget_ns: u64, tx_done: *std.atomic.Value(bool)) u64 {
|
||||||
|
return txWorkerImpl(engine, tmpl, bucket, sink, max_packets, budget_ns, tx_done);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rxWorkerTcp(receiver: *rx.Receiver, clf: rx.TcpClassifier, dd: *dedup.Dedup, sink: *rx.QueueSink, rx_done: *std.atomic.Value(bool)) void {
|
||||||
|
rx.run(receiver, clf, dd, sink);
|
||||||
|
rx_done.store(true, .release);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rxWorkerUdp(receiver: *rx.Receiver, ck: cookie.Cookie, base: u16, span: u16, dd: *dedup.Dedup, sink: *rx.QueueSink, rx_done: *std.atomic.Value(bool)) void {
|
||||||
|
rx.run(receiver, rx.UdpClassifier{ .ck = ck, .base = base, .span = span }, dd, sink);
|
||||||
|
rx_done.store(true, .release);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn svcWorker(
|
||||||
|
engine: *service.Engine,
|
||||||
|
socket: *service.Socket,
|
||||||
|
sink: service.Sink,
|
||||||
|
tx_done: *std.atomic.Value(bool),
|
||||||
|
drain_window_ns: u64,
|
||||||
|
hard_cap_ns: u64,
|
||||||
|
svc_done: *std.atomic.Value(bool),
|
||||||
|
) void {
|
||||||
|
service.run(engine, socket, sink, tx_done, drain_window_ns, hard_cap_ns);
|
||||||
|
svc_done.store(true, .release);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drainFindings(
|
||||||
|
io: std.Io,
|
||||||
|
queue: *std.Io.Queue(service.Finding),
|
||||||
|
buf: []service.Finding,
|
||||||
|
findings: *std.ArrayList(service.Finding),
|
||||||
|
dd: *dedup.Dedup,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
json_out: ?*std.Io.Writer,
|
||||||
|
) void {
|
||||||
|
while (true) {
|
||||||
|
const n = queue.get(io, buf, 0) catch return;
|
||||||
|
if (n == 0) return;
|
||||||
|
for (buf[0..n]) |f| {
|
||||||
|
if (!dd.insert(service.connKey(f.ip, f.port))) continue;
|
||||||
|
findings.append(allocator, f) catch continue;
|
||||||
|
if (json_out) |w| output.emitServiceJson(w, .{
|
||||||
|
.ip = f.ip,
|
||||||
|
.port = f.port,
|
||||||
|
.service = f.info.service,
|
||||||
|
.info = f.info.infoSlice(),
|
||||||
|
.tls = f.info.tls,
|
||||||
|
}) catch {};
|
||||||
|
}
|
||||||
|
if (n < buf.len) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn findingLess(_: void, a: service.Finding, b: service.Finding) bool {
|
||||||
|
if (a.ip != b.ip) return a.ip < b.ip;
|
||||||
|
return a.port < b.port;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn renderServiceTable(out: *std.Io.Writer, level: output.ColorLevel, allocator: std.mem.Allocator, items: []service.Finding) !void {
|
||||||
|
const rows = try allocator.alloc(output.ServiceRow, items.len);
|
||||||
|
for (items, 0..) |*f, i| rows[i] = .{
|
||||||
|
.ip = f.ip,
|
||||||
|
.port = f.port,
|
||||||
|
.service = f.info.service,
|
||||||
|
.info = f.info.infoSlice(),
|
||||||
|
.tls = f.info.tls,
|
||||||
|
};
|
||||||
|
try output.renderServices(out, level, rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn absorb(
|
||||||
|
batch: []const rx.Result,
|
||||||
|
found: *std.ArrayList(rx.Result),
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
stats: *output.Stats,
|
||||||
|
json_out: ?*std.Io.Writer,
|
||||||
|
proto: []const u8,
|
||||||
|
) void {
|
||||||
|
for (batch) |r| {
|
||||||
|
found.append(allocator, r) catch continue;
|
||||||
|
stats.record(r.state);
|
||||||
|
if (json_out) |w| output.emitJson(w, r, proto) catch {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drainQueue(
|
||||||
|
io: std.Io,
|
||||||
|
queue: *std.Io.Queue(rx.Result),
|
||||||
|
buf: []rx.Result,
|
||||||
|
found: *std.ArrayList(rx.Result),
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
stats: *output.Stats,
|
||||||
|
json_out: ?*std.Io.Writer,
|
||||||
|
proto: []const u8,
|
||||||
|
) void {
|
||||||
|
while (true) {
|
||||||
|
const n = queue.get(io, buf, 0) catch return;
|
||||||
|
if (n == 0) return;
|
||||||
|
absorb(buf[0..n], found, allocator, stats, json_out, proto);
|
||||||
|
if (n < buf.len) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn terminalCols(fd: i32) ?u16 {
|
||||||
|
var ws: std.posix.winsize = undefined;
|
||||||
|
const rc = std.os.linux.ioctl(fd, std.os.linux.T.IOCGWINSZ, @intFromPtr(&ws));
|
||||||
|
if (std.os.linux.errno(rc) != .SUCCESS) return null;
|
||||||
|
if (ws.col == 0) return null;
|
||||||
|
return ws.col;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn txWorkerV6(engine: *targets.Engine6, tmpl: *const template.SynTemplate6, bucket: *ratelimit.TokenBucket, sink: *TxSink, max_packets: u64, budget_ns: u64, tx_done: *std.atomic.Value(bool)) u64 {
|
||||||
|
var clock = netutil.RealClock{};
|
||||||
|
const deadline_ns = clock.now() +| budget_ns;
|
||||||
|
const sent = tx.runV6(engine, tmpl, bucket, sink, &clock, max_packets, deadline_ns);
|
||||||
|
tx_done.store(true, .release);
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rxWorkerV6(receiver: *rx.Receiver, clf: rx.TcpClassifier6, dd: *dedup.Dedup, sink: *rx.QueueSink, rx_done: *std.atomic.Value(bool)) void {
|
||||||
|
rx.run(receiver, clf, dd, sink);
|
||||||
|
rx_done.store(true, .release);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolveV6Gateway(io: std.Io, args: []const []const u8, ifname: []const u8, src_ip6: [16]u8, src_mac: [6]u8, derr: *std.Io.Writer) !?[6]u8 {
|
||||||
|
_ = io;
|
||||||
|
if (netutil.getFlag(args, "--gw-mac")) |m| return try netutil.parseMac(m);
|
||||||
|
const next_hop = if (netutil.getFlag(args, "--gw-ip6")) |g| try netutil.parseIpv6(g) else netutil.defaultGateway6(ifname) orelse {
|
||||||
|
try derr.writeAll("scan: IPv6 raw scan needs a next hop: pass --gw-mac <mac>, or --gw-ip6 <addr> to resolve it via NDP\n");
|
||||||
|
try derr.flush();
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
return ndp.resolve(ifname, src_ip6, src_mac, next_hop, 1500) catch |e| {
|
||||||
|
try derr.print("scan: NDP neighbor resolution failed ({s}); pass --gw-mac explicitly\n", .{@errorName(e)});
|
||||||
|
try derr.flush();
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runV6Scan(io: std.Io, allocator: std.mem.Allocator, args: []const []const u8, env: *std.process.Environ.Map, target_text: []const u8, ifname: []const u8, rate: u64, backend_choice: packet_io.Choice, derr: *std.Io.Writer) !void {
|
||||||
|
var obuf: [4096]u8 = undefined;
|
||||||
|
var ow = std.Io.File.stdout().writer(io, &obuf);
|
||||||
|
const out = &ow.interface;
|
||||||
|
|
||||||
|
if (netutil.hasFlag(args, "--udp")) {
|
||||||
|
try derr.writeAll("scan: IPv6 UDP scanning is not supported yet; run a TCP SYN scan or --connect\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (netutil.getFlag(args, "--scan-type") != null or netutil.getFlag(args, "--os-template") != null or
|
||||||
|
netutil.getFlag(args, "--decoys") != null or netutil.getFlag(args, "--jitter") != null or
|
||||||
|
netutil.hasFlag(args, "--source-port-rotation"))
|
||||||
|
{
|
||||||
|
try derr.writeAll(" note: stealth/scan-type options are not yet wired for IPv6; running a plain SYN scan\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const ports = if (netutil.getFlag(args, "--ports")) |p| try netutil.parsePorts(allocator, p) else try allocator.dupe(u16, &default_tcp_ports);
|
||||||
|
const src_port = if (netutil.getFlag(args, "--src-port")) |p| try std.fmt.parseInt(u16, p, 10) else default_src_port;
|
||||||
|
const wait_ms = if (netutil.getFlag(args, "--wait")) |w| try std.fmt.parseInt(i32, w, 10) else default_wait_ms;
|
||||||
|
const json = netutil.hasFlag(args, "--json");
|
||||||
|
const max_hosts = if (netutil.getFlag(args, "--max-hosts")) |m| try std.fmt.parseInt(u64, m, 10) else targets.default_max_hosts6;
|
||||||
|
|
||||||
|
var seed: u64 = undefined;
|
||||||
|
if (netutil.getFlag(args, "--seed")) |s| {
|
||||||
|
seed = try std.fmt.parseInt(u64, s, 10);
|
||||||
|
} else {
|
||||||
|
var seed_bytes: [8]u8 = undefined;
|
||||||
|
try io.randomSecure(&seed_bytes);
|
||||||
|
seed = std.mem.readInt(u64, &seed_bytes, .little);
|
||||||
|
}
|
||||||
|
|
||||||
|
const src_ip6 = if (netutil.getFlag(args, "--src-ip6")) |s| try netutil.parseIpv6(s) else netutil.resolveSrcIp6(ifname) catch {
|
||||||
|
try derr.print("scan: no IPv6 source address on '{s}' (assign one, or pass --src-ip6 <addr>)\n", .{ifname});
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const src_mac = try netutil.resolveSrcMac(ifname);
|
||||||
|
const gw_mac = (try resolveV6Gateway(io, args, ifname, src_ip6, src_mac, derr)) orelse return;
|
||||||
|
|
||||||
|
const cidr = targets.parseCidr6(target_text) catch |e| {
|
||||||
|
try derr.print("scan: invalid IPv6 target '{s}' ({s})\n", .{ target_text, @errorName(e) });
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
var eng = targets.Engine6.init(allocator, cidr, ports, seed, max_hosts) catch |e| switch (e) {
|
||||||
|
error.PrefixTooLarge => {
|
||||||
|
try derr.print("scan: IPv6 prefix '{s}' is too large to enumerate (over --max-hosts {d}); narrow the prefix or raise --max-hosts\n", .{ target_text, max_hosts });
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return e,
|
||||||
|
};
|
||||||
|
defer eng.deinit();
|
||||||
|
const count = if (netutil.getFlag(args, "--count")) |c| try std.fmt.parseInt(u64, c, 10) else eng.total;
|
||||||
|
|
||||||
|
const ck = try cookie.Cookie.random(io);
|
||||||
|
const tmpl = template.SynTemplate6.init(.{
|
||||||
|
.src_mac = src_mac,
|
||||||
|
.dst_mac = gw_mac,
|
||||||
|
.src_ip = src_ip6,
|
||||||
|
.src_port = src_port,
|
||||||
|
.cookie = ck,
|
||||||
|
});
|
||||||
|
var bucket = ratelimit.TokenBucket.init(rate, rate);
|
||||||
|
|
||||||
|
const choice = output.parseColorChoice(netutil.getFlag(args, "--color"));
|
||||||
|
const out_level = output.envLevel(io, std.Io.File.stdout(), env, choice);
|
||||||
|
const err_level = output.envLevel(io, std.Io.File.stderr(), env, choice);
|
||||||
|
const stderr_tty = std.Io.File.stderr().isTty(io) catch false;
|
||||||
|
const wide_enough = if (terminalCols(2)) |c| c >= min_dashboard_cols else true;
|
||||||
|
const interactive = stderr_tty and wide_enough;
|
||||||
|
|
||||||
|
const dash_total = @min(count, eng.total);
|
||||||
|
const drain_window_ns: u64 = @as(u64, @intCast(@max(wait_ms, 0))) * ns_per_ms;
|
||||||
|
const est_tx_ns: u64 = if (rate > 0) (dash_total / rate) *| ns_per_sec else rx_hard_cap_floor_ns;
|
||||||
|
const tx_budget_ns: u64 = (est_tx_ns *| 4) +| rx_hard_cap_floor_ns;
|
||||||
|
const hard_cap_ns: u64 = tx_budget_ns +| drain_window_ns;
|
||||||
|
|
||||||
|
var backend = packet_io.select(allocator, ifname, backend_choice, .{}, .{}, derr) catch |err| switch (err) {
|
||||||
|
error.NeedCapNetRaw => {
|
||||||
|
try derr.writeAll(need_cap_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.XdpNotCompiledIn => {
|
||||||
|
try derr.writeAll("scan: --backend xdp needs a build with -Dxdp\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer backend.close();
|
||||||
|
try derr.print(" using {s}\n", .{packet_io.kindLabel(backend.kind())});
|
||||||
|
|
||||||
|
var tx_done = std.atomic.Value(bool).init(false);
|
||||||
|
var rx_done = std.atomic.Value(bool).init(false);
|
||||||
|
|
||||||
|
var receiver = rx.Receiver.open(ifname, rx.ETH_P_IPV6, &tx_done, drain_window_ns, hard_cap_ns) catch |err| switch (err) {
|
||||||
|
error.NeedCapNetRaw => {
|
||||||
|
try derr.writeAll(need_cap_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer receiver.close();
|
||||||
|
|
||||||
|
var dd = try dedup.Dedup.init(allocator, dedup_capacity);
|
||||||
|
defer dd.deinit();
|
||||||
|
var qbuf: [queue_capacity]rx.Result = undefined;
|
||||||
|
var queue = std.Io.Queue(rx.Result).init(&qbuf);
|
||||||
|
|
||||||
|
var found_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
|
defer found_arena.deinit();
|
||||||
|
const found_alloc = found_arena.allocator();
|
||||||
|
var found: std.ArrayList(rx.Result) = .empty;
|
||||||
|
|
||||||
|
var stats: output.Stats = .{};
|
||||||
|
const json_out: ?*std.Io.Writer = if (json) out else null;
|
||||||
|
|
||||||
|
try derr.print("zingela SYN scan (IPv6) target {s} iface {s} rate {d} pps ports {d}\n", .{ target_text, ifname, rate, ports.len });
|
||||||
|
try derr.flush();
|
||||||
|
|
||||||
|
var clock = netutil.RealClock{};
|
||||||
|
const t0 = clock.now();
|
||||||
|
|
||||||
|
var tx_sink = TxSink{ .backend = &backend, .sent = &stats.sent.v };
|
||||||
|
var rx_sink = rx.QueueSink{ .queue = &queue, .io = io };
|
||||||
|
|
||||||
|
var tx_fut = (io.concurrent(txWorkerV6, .{ &eng, &tmpl, &bucket, &tx_sink, count, tx_budget_ns, &tx_done })) catch {
|
||||||
|
try derr.writeAll(concurrency_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
var rx_fut = (io.concurrent(rxWorkerV6, .{ &receiver, rx.TcpClassifier6{ .ck = ck }, &dd, &rx_sink, &rx_done })) catch {
|
||||||
|
_ = tx_fut.await(io);
|
||||||
|
try derr.writeAll(concurrency_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
var dash = output.Dashboard.init(err_level, interactive, dash_total);
|
||||||
|
const render_interval_ns: u64 = if (interactive) render_tick_interactive_ns else render_tick_plain_ns;
|
||||||
|
var drain_buf: [drain_batch]rx.Result = undefined;
|
||||||
|
var last_render: u64 = 0;
|
||||||
|
|
||||||
|
while (!(tx_done.load(.acquire) and rx_done.load(.acquire))) {
|
||||||
|
drainQueue(io, &queue, drain_buf[0..], &found, found_alloc, &stats, json_out, "tcp");
|
||||||
|
if (json) out.flush() catch {};
|
||||||
|
const now = clock.now();
|
||||||
|
if (last_render == 0 or now -| last_render >= render_interval_ns) {
|
||||||
|
dash.render(derr, &stats, now -| t0) catch {};
|
||||||
|
last_render = now;
|
||||||
|
}
|
||||||
|
clock.sleepNs(drain_tick_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sent = tx_fut.await(io);
|
||||||
|
rx_fut.await(io);
|
||||||
|
queue.close(io);
|
||||||
|
drainQueue(io, &queue, drain_buf[0..], &found, found_alloc, &stats, json_out, "tcp");
|
||||||
|
if (json) out.flush() catch {};
|
||||||
|
dash.render(derr, &stats, clock.now() -| t0) catch {};
|
||||||
|
|
||||||
|
var open_n: u64 = 0;
|
||||||
|
var closed_n: u64 = 0;
|
||||||
|
var filtered_n: u64 = 0;
|
||||||
|
var unfiltered_n: u64 = 0;
|
||||||
|
for (found.items) |r| switch (r.state) {
|
||||||
|
.open => open_n += 1,
|
||||||
|
.closed => closed_n += 1,
|
||||||
|
.filtered => filtered_n += 1,
|
||||||
|
.unfiltered => unfiltered_n += 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!json) {
|
||||||
|
if (found.items.len > 0) {
|
||||||
|
std.mem.sort(rx.Result, found.items, {}, output.ipPortLess);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
try output.renderTable(out, out_level, found.items);
|
||||||
|
try out.flush();
|
||||||
|
} else {
|
||||||
|
try derr.writeAll(" no open, closed, or filtered responses observed\n");
|
||||||
|
if (sent > 0) try derr.writeAll(" (if a cloud/VM hypervisor is filtering raw sends upstream, retry with --connect)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed_s = @as(f64, @floatFromInt(clock.now() - t0)) / @as(f64, @floatFromInt(ns_per_sec));
|
||||||
|
try derr.writeByte('\n');
|
||||||
|
try output.renderSummary(derr, err_level, sent, "SYN", ifname, elapsed_s, open_n, closed_n, filtered_n, unfiltered_n, 0);
|
||||||
|
try derr.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runConnect(
|
||||||
|
io: std.Io,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
args: []const []const u8,
|
||||||
|
env: *std.process.Environ.Map,
|
||||||
|
target_text: []const u8,
|
||||||
|
ifname: []const u8,
|
||||||
|
rate: u64,
|
||||||
|
is_udp: bool,
|
||||||
|
json: bool,
|
||||||
|
derr: *std.Io.Writer,
|
||||||
|
) !void {
|
||||||
|
if (is_udp) {
|
||||||
|
try derr.writeAll("scan: connect mode is TCP-only; --udp is not supported with --backend connect\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (netutil.hasFlag(args, "--banners")) {
|
||||||
|
try derr.writeAll(" note: --banners is unavailable in connect mode; reporting open/closed/filtered only\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const ports = if (netutil.getFlag(args, "--ports")) |p|
|
||||||
|
try netutil.parsePorts(allocator, p)
|
||||||
|
else
|
||||||
|
try allocator.dupe(u16, &default_tcp_ports);
|
||||||
|
|
||||||
|
var seed: u64 = undefined;
|
||||||
|
if (netutil.getFlag(args, "--seed")) |s| {
|
||||||
|
seed = try std.fmt.parseInt(u64, s, 10);
|
||||||
|
} else {
|
||||||
|
var seed_bytes: [8]u8 = undefined;
|
||||||
|
try io.randomSecure(&seed_bytes);
|
||||||
|
seed = std.mem.readInt(u64, &seed_bytes, .little);
|
||||||
|
}
|
||||||
|
|
||||||
|
const concurrency = if (netutil.getFlag(args, "--concurrency")) |c| try std.fmt.parseInt(usize, c, 10) else connect.default_concurrency;
|
||||||
|
const timeout_ms = if (netutil.getFlag(args, "--connect-timeout")) |t| try std.fmt.parseInt(u64, t, 10) else connect.default_timeout_ms;
|
||||||
|
const max_hosts = if (netutil.getFlag(args, "--max-hosts")) |m| try std.fmt.parseInt(u64, m, 10) else targets.default_max_hosts6;
|
||||||
|
|
||||||
|
const choice = output.parseColorChoice(netutil.getFlag(args, "--color"));
|
||||||
|
const out_level = output.envLevel(io, std.Io.File.stdout(), env, choice);
|
||||||
|
const err_level = output.envLevel(io, std.Io.File.stderr(), env, choice);
|
||||||
|
const stderr_tty = std.Io.File.stderr().isTty(io) catch false;
|
||||||
|
const wide_enough = if (terminalCols(2)) |c| c >= min_dashboard_cols else true;
|
||||||
|
const interactive = stderr_tty and wide_enough;
|
||||||
|
try derr.flush();
|
||||||
|
|
||||||
|
if (std.mem.indexOfScalar(u8, target_text, ':') != null) {
|
||||||
|
const cidr = targets.parseCidr6(target_text) catch |e| {
|
||||||
|
try derr.print("scan: invalid IPv6 target '{s}' ({s})\n", .{ target_text, @errorName(e) });
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
var eng = targets.Engine6.init(allocator, cidr, ports, seed, max_hosts) catch |e| switch (e) {
|
||||||
|
error.PrefixTooLarge => {
|
||||||
|
try derr.print("scan: IPv6 prefix '{s}' is too large to enumerate (over --max-hosts {d}); narrow the prefix or raise --max-hosts\n", .{ target_text, max_hosts });
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return e,
|
||||||
|
};
|
||||||
|
defer eng.deinit();
|
||||||
|
const count = if (netutil.getFlag(args, "--count")) |c| try std.fmt.parseInt(u64, c, 10) else eng.total;
|
||||||
|
try connect.run(allocator, .{
|
||||||
|
.source = .{ .v6 = &eng },
|
||||||
|
.count = count,
|
||||||
|
.total = @min(count, eng.total),
|
||||||
|
.rate = rate,
|
||||||
|
.concurrency = concurrency,
|
||||||
|
.timeout_ns = timeout_ms *| ns_per_ms,
|
||||||
|
.out_level = out_level,
|
||||||
|
.err_level = err_level,
|
||||||
|
.interactive = interactive,
|
||||||
|
.json = json,
|
||||||
|
.target_text = target_text,
|
||||||
|
.iface = ifname,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cidr = try targets.parseCidr(target_text);
|
||||||
|
var eng = try targets.Engine.init(allocator, &.{cidr}, ports, seed);
|
||||||
|
defer eng.deinit();
|
||||||
|
const count = if (netutil.getFlag(args, "--count")) |c| try std.fmt.parseInt(u64, c, 10) else eng.total;
|
||||||
|
|
||||||
|
try connect.run(allocator, .{
|
||||||
|
.source = .{ .v4 = &eng },
|
||||||
|
.count = count,
|
||||||
|
.total = @min(count, eng.total),
|
||||||
|
.rate = rate,
|
||||||
|
.concurrency = concurrency,
|
||||||
|
.timeout_ns = timeout_ms *| ns_per_ms,
|
||||||
|
.out_level = out_level,
|
||||||
|
.err_level = err_level,
|
||||||
|
.interactive = interactive,
|
||||||
|
.json = json,
|
||||||
|
.target_text = target_text,
|
||||||
|
.iface = ifname,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(io: std.Io, allocator: std.mem.Allocator, args: []const []const u8, env: *std.process.Environ.Map) !void {
|
||||||
|
var obuf: [4096]u8 = undefined;
|
||||||
|
var ow = std.Io.File.stdout().writer(io, &obuf);
|
||||||
|
const out = &ow.interface;
|
||||||
|
|
||||||
|
var ebuf: [4096]u8 = undefined;
|
||||||
|
var ew = std.Io.File.stderr().writer(io, &ebuf);
|
||||||
|
const derr = &ew.interface;
|
||||||
|
|
||||||
|
const target_text = netutil.getFlag(args, "--target") orelse {
|
||||||
|
try derr.writeAll("scan: --target <cidr> is required (e.g. --target 10.0.0.0/24)\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const ifname = netutil.getFlag(args, "--iface") orelse default_iface;
|
||||||
|
const rate = if (netutil.getFlag(args, "--rate")) |r| try std.fmt.parseInt(u64, r, 10) else default_rate;
|
||||||
|
const src_port = if (netutil.getFlag(args, "--src-port")) |p| try std.fmt.parseInt(u16, p, 10) else default_src_port;
|
||||||
|
const wait_ms = if (netutil.getFlag(args, "--wait")) |w| try std.fmt.parseInt(i32, w, 10) else default_wait_ms;
|
||||||
|
const json = netutil.hasFlag(args, "--json");
|
||||||
|
const is_udp = netutil.hasFlag(args, "--udp");
|
||||||
|
const banners_flag = netutil.hasFlag(args, "--banners");
|
||||||
|
|
||||||
|
const backend_arg = netutil.getFlag(args, "--backend");
|
||||||
|
const connect_mode = netutil.hasFlag(args, "--connect") or
|
||||||
|
(backend_arg != null and std.mem.eql(u8, backend_arg.?, "connect"));
|
||||||
|
if (connect_mode) {
|
||||||
|
return runConnect(io, allocator, args, env, target_text, ifname, rate, is_udp, json, derr);
|
||||||
|
}
|
||||||
|
|
||||||
|
const backend_choice = packet_io.parseChoice(backend_arg) orelse {
|
||||||
|
try derr.writeAll("scan: --backend must be one of auto, xdp, afpacket, connect\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (backend_choice == .auto) {
|
||||||
|
const raw_status = rawprobe.probe(ifname);
|
||||||
|
if (raw_status != .ok) {
|
||||||
|
try derr.print(" raw sends unavailable on '{s}' ({s}); falling back to unprivileged connect scan\n", .{ ifname, raw_status.reason() });
|
||||||
|
try derr.writeAll(" (force the raw engine with --backend afpacket, or select it directly with --connect)\n");
|
||||||
|
try derr.flush();
|
||||||
|
return runConnect(io, allocator, args, env, target_text, ifname, rate, is_udp, json, derr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std.mem.indexOfScalar(u8, target_text, ':') != null) {
|
||||||
|
return runV6Scan(io, allocator, args, env, target_text, ifname, rate, backend_choice, derr);
|
||||||
|
}
|
||||||
|
|
||||||
|
var scfg = stealth.parse(allocator, io, args) catch |e| switch (e) {
|
||||||
|
error.AuthorizationRequired => {
|
||||||
|
try derr.writeAll(authorized_warning);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.BadOsTemplate => {
|
||||||
|
try derr.writeAll("scan: --os-template must be none, masscan, linux, windows, or macos\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.BadScanType => {
|
||||||
|
try derr.writeAll("scan: --scan-type must be syn, fin, null, xmas, maimon, ack, or window\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.BadJitterMode => {
|
||||||
|
try derr.writeAll("scan: --jitter must be poisson or none\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.BadDecoySpec => {
|
||||||
|
try derr.writeAll("scan: --decoys must be comma-separated IPv4 addresses and/or RND:N\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.TooManyDecoys => {
|
||||||
|
try derr.print("scan: at most {d} decoys allowed\n", .{stealth.max_decoys});
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.OutOfMemory => return e,
|
||||||
|
};
|
||||||
|
defer scfg.deinit(allocator);
|
||||||
|
|
||||||
|
if (is_udp and (scfg.profile != .none or scfg.scan != .syn or scfg.rotate or scfg.decoys.len > 0 or scfg.suppress_rst)) {
|
||||||
|
try derr.writeAll(" note: --os-template/--scan-type/--source-port-rotation/--decoys/--suppress-rst apply to TCP scans; ignored for --udp\n");
|
||||||
|
}
|
||||||
|
if (is_udp and banners_flag) {
|
||||||
|
try derr.writeAll(" note: --banners is a TCP feature; ignored for --udp\n");
|
||||||
|
}
|
||||||
|
const banners = banners_flag and !is_udp and scfg.scan == .syn;
|
||||||
|
if (banners_flag and !is_udp and scfg.scan != .syn) {
|
||||||
|
try derr.writeAll(" note: --banners needs a full handshake; ignored for non-SYN scan types\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
const udp_base: u16 = src_port;
|
||||||
|
const udp_span: u16 = @intCast(@min(@as(u32, default_udp_src_span), 65536 - @as(u32, udp_base)));
|
||||||
|
const proto_json: []const u8 = if (is_udp) "udp" else "tcp";
|
||||||
|
const probe_label: []const u8 = if (is_udp) "UDP" else "SYN";
|
||||||
|
|
||||||
|
const ports = if (netutil.getFlag(args, "--ports")) |p|
|
||||||
|
try netutil.parsePorts(allocator, p)
|
||||||
|
else if (is_udp)
|
||||||
|
try allocator.dupe(u16, &default_udp_ports)
|
||||||
|
else
|
||||||
|
try allocator.dupe(u16, &default_tcp_ports);
|
||||||
|
const gw_mac = if (netutil.getFlag(args, "--gw-mac")) |m| try netutil.parseMac(m) else [_]u8{0} ** 6;
|
||||||
|
const src_ip = if (netutil.getFlag(args, "--src-ip")) |s| try netutil.parseIpv4(s) else try netutil.resolveSrcIp(ifname);
|
||||||
|
const src_mac = try netutil.resolveSrcMac(ifname);
|
||||||
|
|
||||||
|
const choice = output.parseColorChoice(netutil.getFlag(args, "--color"));
|
||||||
|
const out_level = output.envLevel(io, std.Io.File.stdout(), env, choice);
|
||||||
|
const err_level = output.envLevel(io, std.Io.File.stderr(), env, choice);
|
||||||
|
const stderr_tty = std.Io.File.stderr().isTty(io) catch false;
|
||||||
|
const wide_enough = if (terminalCols(2)) |c| c >= min_dashboard_cols else true;
|
||||||
|
const interactive = stderr_tty and wide_enough;
|
||||||
|
|
||||||
|
var seed: u64 = undefined;
|
||||||
|
if (netutil.getFlag(args, "--seed")) |s| {
|
||||||
|
seed = try std.fmt.parseInt(u64, s, 10);
|
||||||
|
} else {
|
||||||
|
var seed_bytes: [8]u8 = undefined;
|
||||||
|
try io.randomSecure(&seed_bytes);
|
||||||
|
seed = std.mem.readInt(u64, &seed_bytes, .little);
|
||||||
|
}
|
||||||
|
|
||||||
|
const cidr = try targets.parseCidr(target_text);
|
||||||
|
var eng = try targets.Engine.init(allocator, &.{cidr}, ports, seed);
|
||||||
|
defer eng.deinit();
|
||||||
|
const count = if (netutil.getFlag(args, "--count")) |c| try std.fmt.parseInt(u64, c, 10) else eng.total;
|
||||||
|
const frames_per_probe: u64 = if (is_udp) 1 else 1 + @as(u64, @intCast(scfg.decoys.len));
|
||||||
|
const dash_total = @min(count, eng.total) *| frames_per_probe;
|
||||||
|
|
||||||
|
const ck = try cookie.Cookie.random(io);
|
||||||
|
const rot_span: u16 = if (scfg.rotate) @intCast(@min(@as(u32, scfg.rotate_span), 65536 - @as(u32, src_port))) else 0;
|
||||||
|
const tcp_tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = src_mac,
|
||||||
|
.dst_mac = gw_mac,
|
||||||
|
.src_ip = src_ip,
|
||||||
|
.src_port = src_port,
|
||||||
|
.cookie = ck,
|
||||||
|
.profile = scfg.profile,
|
||||||
|
.scan = scfg.scan,
|
||||||
|
.rotate = scfg.rotate,
|
||||||
|
.rotate_base = src_port,
|
||||||
|
.rotate_span = rot_span,
|
||||||
|
.decoys = scfg.decoys,
|
||||||
|
});
|
||||||
|
const udp_tmpl = udp.UdpTemplate.init(.{
|
||||||
|
.src_mac = src_mac,
|
||||||
|
.dst_mac = gw_mac,
|
||||||
|
.src_ip = src_ip,
|
||||||
|
.src_port_base = udp_base,
|
||||||
|
.src_port_span = udp_span,
|
||||||
|
.cookie = ck,
|
||||||
|
});
|
||||||
|
var bucket = ratelimit.TokenBucket.init(rate, rate);
|
||||||
|
if (scfg.jitter) bucket = bucket.withJitter(seed);
|
||||||
|
|
||||||
|
var backend = packet_io.select(allocator, ifname, backend_choice, .{}, .{}, derr) catch |err| switch (err) {
|
||||||
|
error.NeedCapNetRaw => {
|
||||||
|
try derr.writeAll(need_cap_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.XdpNotCompiledIn => {
|
||||||
|
try derr.writeAll("scan: --backend xdp needs a build with -Dxdp\n");
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer backend.close();
|
||||||
|
try derr.print(" using {s}\n", .{packet_io.kindLabel(backend.kind())});
|
||||||
|
|
||||||
|
if (scfg.profile != .none or scfg.scan != .syn or scfg.jitter or scfg.rotate or scfg.decoys.len > 0) {
|
||||||
|
try derr.print(" stealth: template={s} scan={s} jitter={s} rotate={s} decoys={d}\n", .{
|
||||||
|
@tagName(scfg.profile),
|
||||||
|
@tagName(scfg.scan),
|
||||||
|
if (scfg.jitter) "on" else "off",
|
||||||
|
if (scfg.rotate) "on" else "off",
|
||||||
|
scfg.decoys.len,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var supp: ?stealth.RstSuppressor = null;
|
||||||
|
if ((scfg.suppress_rst or banners) and !is_udp) {
|
||||||
|
const lo = src_port;
|
||||||
|
const hi = if (scfg.rotate) src_port +| (rot_span -| 1) else src_port;
|
||||||
|
supp = stealth.RstSuppressor.install(allocator, io, src_ip, lo, hi) catch |e| blk: {
|
||||||
|
try derr.print(" note: RST-suppression unavailable ({s})", .{@errorName(e)});
|
||||||
|
if (banners) try derr.writeAll("; banner grabs may be unreliable (the kernel RSTs the SYN-ACK)");
|
||||||
|
try derr.writeByte('\n');
|
||||||
|
break :blk null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
defer if (supp) |*s| s.teardown();
|
||||||
|
if (supp) |*s| {
|
||||||
|
var hbuf: [160]u8 = undefined;
|
||||||
|
try derr.print(" RST-suppression active (cleanup if hard-killed: {s})\n", .{s.cleanupHint(&hbuf)});
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx_done = std.atomic.Value(bool).init(false);
|
||||||
|
var rx_done = std.atomic.Value(bool).init(false);
|
||||||
|
|
||||||
|
const drain_window_ns: u64 = @as(u64, @intCast(@max(wait_ms, 0))) * ns_per_ms;
|
||||||
|
const est_tx_ns: u64 = if (rate > 0) (dash_total / rate) *| ns_per_sec else rx_hard_cap_floor_ns;
|
||||||
|
const tx_budget_ns: u64 = (est_tx_ns *| 4) +| rx_hard_cap_floor_ns;
|
||||||
|
const hard_cap_ns: u64 = tx_budget_ns +| drain_window_ns;
|
||||||
|
|
||||||
|
const banner_wait_ns: u64 = service.default_banner_wait_ns;
|
||||||
|
const svc_drain_window_ns: u64 = @max(drain_window_ns, banner_wait_ns +| ns_per_sec);
|
||||||
|
const svc_hard_cap_ns: u64 = tx_budget_ns +| svc_drain_window_ns;
|
||||||
|
|
||||||
|
var banners_active = banners;
|
||||||
|
var svc_socket: service.Socket = undefined;
|
||||||
|
var svc_socket_open = false;
|
||||||
|
var svc_engine: ?*service.Engine = null;
|
||||||
|
if (banners_active) {
|
||||||
|
if (service.Socket.open(ifname)) |s| {
|
||||||
|
svc_socket = s;
|
||||||
|
svc_socket_open = true;
|
||||||
|
errdefer svc_socket.close();
|
||||||
|
const svc_eng = try allocator.create(service.Engine);
|
||||||
|
svc_eng.* = service.Engine.init(.{
|
||||||
|
.cookie = ck,
|
||||||
|
.our_ip = src_ip,
|
||||||
|
.src_mac = src_mac,
|
||||||
|
.gw_mac = gw_mac,
|
||||||
|
.banner_wait_ns = banner_wait_ns,
|
||||||
|
});
|
||||||
|
svc_engine = svc_eng;
|
||||||
|
} else |e| {
|
||||||
|
try derr.print(" note: banner engine socket unavailable ({s}); continuing port-scan only\n", .{@errorName(e)});
|
||||||
|
banners_active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer if (svc_socket_open) svc_socket.close();
|
||||||
|
|
||||||
|
var fqbuf: [finding_queue_capacity]service.Finding = undefined;
|
||||||
|
var finding_queue = std.Io.Queue(service.Finding).init(&fqbuf);
|
||||||
|
var finding_sink = FindingSink{ .queue = &finding_queue, .io = io };
|
||||||
|
var svc_done = std.atomic.Value(bool).init(true);
|
||||||
|
var findings: std.ArrayList(service.Finding) = .empty;
|
||||||
|
var fdedup = try dedup.Dedup.init(allocator, dedup_capacity);
|
||||||
|
defer fdedup.deinit();
|
||||||
|
|
||||||
|
var receiver = rx.Receiver.open(ifname, rx.ETH_P_IP, &tx_done, drain_window_ns, hard_cap_ns) catch |err| switch (err) {
|
||||||
|
error.NeedCapNetRaw => {
|
||||||
|
try derr.writeAll(need_cap_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer receiver.close();
|
||||||
|
|
||||||
|
var dd = try dedup.Dedup.init(allocator, dedup_capacity);
|
||||||
|
defer dd.deinit();
|
||||||
|
|
||||||
|
var qbuf: [queue_capacity]rx.Result = undefined;
|
||||||
|
var queue = std.Io.Queue(rx.Result).init(&qbuf);
|
||||||
|
|
||||||
|
var found_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||||
|
defer found_arena.deinit();
|
||||||
|
const found_alloc = found_arena.allocator();
|
||||||
|
var found: std.ArrayList(rx.Result) = .empty;
|
||||||
|
|
||||||
|
var stats: output.Stats = .{};
|
||||||
|
const json_out: ?*std.Io.Writer = if (json) out else null;
|
||||||
|
|
||||||
|
try derr.print("zingela {s} scan target {s} iface {s} rate {d} pps ports {d}\n", .{ probe_label, target_text, ifname, rate, ports.len });
|
||||||
|
try derr.flush();
|
||||||
|
|
||||||
|
var clock = netutil.RealClock{};
|
||||||
|
const t0 = clock.now();
|
||||||
|
|
||||||
|
var tx_sink = TxSink{ .backend = &backend, .sent = &stats.sent.v };
|
||||||
|
var rx_sink = rx.QueueSink{ .queue = &queue, .io = io };
|
||||||
|
|
||||||
|
const tx_res = if (is_udp)
|
||||||
|
io.concurrent(txWorkerUdp, .{ &eng, &udp_tmpl, &bucket, &tx_sink, count, tx_budget_ns, &tx_done })
|
||||||
|
else
|
||||||
|
io.concurrent(txWorkerTcp, .{ &eng, &tcp_tmpl, &bucket, &tx_sink, count, tx_budget_ns, &tx_done });
|
||||||
|
var tx_fut = tx_res catch {
|
||||||
|
try derr.writeAll(concurrency_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const rx_res = if (is_udp)
|
||||||
|
io.concurrent(rxWorkerUdp, .{ &receiver, ck, udp_base, udp_span, &dd, &rx_sink, &rx_done })
|
||||||
|
else
|
||||||
|
io.concurrent(rxWorkerTcp, .{ &receiver, rx.TcpClassifier{ .ck = ck, .scan = scfg.scan }, &dd, &rx_sink, &rx_done });
|
||||||
|
var rx_fut = rx_res catch {
|
||||||
|
_ = tx_fut.await(io);
|
||||||
|
try derr.writeAll(concurrency_hint);
|
||||||
|
try derr.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
const SvcFuture = @typeInfo(@TypeOf(io.concurrent(svcWorker, .{
|
||||||
|
svc_engine orelse undefined, &svc_socket, finding_sink.sink(), &tx_done, svc_drain_window_ns, svc_hard_cap_ns, &svc_done,
|
||||||
|
}))).error_union.payload;
|
||||||
|
var svc_fut: ?SvcFuture = null;
|
||||||
|
if (banners_active) {
|
||||||
|
svc_done.store(false, .release);
|
||||||
|
if (io.concurrent(svcWorker, .{
|
||||||
|
svc_engine.?, &svc_socket, finding_sink.sink(), &tx_done, svc_drain_window_ns, svc_hard_cap_ns, &svc_done,
|
||||||
|
})) |f| {
|
||||||
|
svc_fut = f;
|
||||||
|
} else |_| {
|
||||||
|
svc_done.store(true, .release);
|
||||||
|
banners_active = false;
|
||||||
|
try derr.writeAll(" note: banner engine could not launch a worker thread; continuing port-scan only\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var dash = output.Dashboard.init(err_level, interactive, dash_total);
|
||||||
|
dash.banners_mode = banners_active;
|
||||||
|
const render_interval_ns: u64 = if (interactive) render_tick_interactive_ns else render_tick_plain_ns;
|
||||||
|
var drain_buf: [drain_batch]rx.Result = undefined;
|
||||||
|
var finding_buf: [finding_drain_batch]service.Finding = undefined;
|
||||||
|
var last_render: u64 = 0;
|
||||||
|
|
||||||
|
while (!(tx_done.load(.acquire) and rx_done.load(.acquire) and svc_done.load(.acquire))) {
|
||||||
|
drainQueue(io, &queue, drain_buf[0..], &found, found_alloc, &stats, json_out, proto_json);
|
||||||
|
if (banners_active) {
|
||||||
|
drainFindings(io, &finding_queue, finding_buf[0..], &findings, &fdedup, found_alloc, json_out);
|
||||||
|
stats.banners.v.store(svc_engine.?.banners.load(.monotonic), .monotonic);
|
||||||
|
}
|
||||||
|
if (json) out.flush() catch {};
|
||||||
|
const now = clock.now();
|
||||||
|
if (last_render == 0 or now -| last_render >= render_interval_ns) {
|
||||||
|
dash.render(derr, &stats, now -| t0) catch {};
|
||||||
|
last_render = now;
|
||||||
|
}
|
||||||
|
clock.sleepNs(drain_tick_ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sent = tx_fut.await(io);
|
||||||
|
rx_fut.await(io);
|
||||||
|
if (svc_fut) |*f| f.await(io);
|
||||||
|
queue.close(io);
|
||||||
|
drainQueue(io, &queue, drain_buf[0..], &found, found_alloc, &stats, json_out, proto_json);
|
||||||
|
if (banners_active) {
|
||||||
|
finding_queue.close(io);
|
||||||
|
drainFindings(io, &finding_queue, finding_buf[0..], &findings, &fdedup, found_alloc, json_out);
|
||||||
|
stats.banners.v.store(svc_engine.?.banners.load(.monotonic), .monotonic);
|
||||||
|
}
|
||||||
|
if (json) out.flush() catch {};
|
||||||
|
|
||||||
|
dash.render(derr, &stats, clock.now() -| t0) catch {};
|
||||||
|
|
||||||
|
var open_n: u64 = 0;
|
||||||
|
var closed_n: u64 = 0;
|
||||||
|
var filtered_n: u64 = 0;
|
||||||
|
var unfiltered_n: u64 = 0;
|
||||||
|
for (found.items) |r| switch (r.state) {
|
||||||
|
.open => open_n += 1,
|
||||||
|
.closed => closed_n += 1,
|
||||||
|
.filtered => filtered_n += 1,
|
||||||
|
.unfiltered => unfiltered_n += 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!json) {
|
||||||
|
if (found.items.len > 0) {
|
||||||
|
std.mem.sort(rx.Result, found.items, {}, output.ipPortLess);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
try output.renderTable(out, out_level, found.items);
|
||||||
|
try out.flush();
|
||||||
|
} else {
|
||||||
|
try derr.writeAll(" no open, closed, or filtered responses observed\n");
|
||||||
|
if (sent > 0) try derr.writeAll(" (if a cloud/VM hypervisor is filtering raw sends upstream, retry with --connect)\n");
|
||||||
|
}
|
||||||
|
if (findings.items.len > 0) {
|
||||||
|
std.mem.sort(service.Finding, findings.items, {}, findingLess);
|
||||||
|
try out.writeByte('\n');
|
||||||
|
try renderServiceTable(out, out_level, found_alloc, findings.items);
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed_s = @as(f64, @floatFromInt(clock.now() - t0)) / @as(f64, @floatFromInt(ns_per_sec));
|
||||||
|
try derr.writeByte('\n');
|
||||||
|
try output.renderSummary(derr, err_level, sent, probe_label, ifname, elapsed_s, open_n, closed_n, filtered_n, unfiltered_n, stats.banners.v.load(.monotonic));
|
||||||
|
if (banners_active) {
|
||||||
|
const drops = svc_engine.?.drops.load(.monotonic);
|
||||||
|
if (drops > 0) try derr.print(" note: {d} banner connection(s) dropped under backpressure (conn-table full at {d})\n", .{ drops, service.default_capacity });
|
||||||
|
}
|
||||||
|
if (is_udp) {
|
||||||
|
const answered = open_n + closed_n + filtered_n + unfiltered_n;
|
||||||
|
try output.renderUnanswered(derr, err_level, sent -| answered);
|
||||||
|
}
|
||||||
|
try derr.flush();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,183 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// segment.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
const eth_len: usize = 14;
|
||||||
|
const ip_len: usize = 20;
|
||||||
|
const tcp_len: usize = 20;
|
||||||
|
const l4_off: usize = eth_len + ip_len;
|
||||||
|
const payload_off: usize = l4_off + tcp_len;
|
||||||
|
|
||||||
|
const ip_checksum_off: usize = eth_len + 10;
|
||||||
|
const tcp_checksum_off: usize = l4_off + 16;
|
||||||
|
|
||||||
|
const ethertype_ipv4: u16 = 0x0800;
|
||||||
|
const ipv4_version_ihl: u8 = 0x45;
|
||||||
|
const ip_proto_tcp: u8 = 6;
|
||||||
|
const ip_flag_dont_fragment: u16 = 0x4000;
|
||||||
|
const default_ttl: u8 = 64;
|
||||||
|
const tcp_data_off_5words: u8 = 0x50;
|
||||||
|
const default_window: u16 = 65535;
|
||||||
|
|
||||||
|
pub const max_payload: usize = 128;
|
||||||
|
pub const max_len: usize = payload_off + max_payload;
|
||||||
|
|
||||||
|
pub const Params = struct {
|
||||||
|
src_mac: [6]u8,
|
||||||
|
dst_mac: [6]u8,
|
||||||
|
src_ip: u32,
|
||||||
|
dst_ip: u32,
|
||||||
|
src_port: u16,
|
||||||
|
dst_port: u16,
|
||||||
|
seq: u32,
|
||||||
|
ack: u32,
|
||||||
|
flags: u8,
|
||||||
|
window: u16 = default_window,
|
||||||
|
payload: []const u8 = &.{},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn build(out: *[max_len]u8, p: Params) usize {
|
||||||
|
std.debug.assert(p.payload.len <= max_payload);
|
||||||
|
const total = payload_off + p.payload.len;
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = p.dst_mac,
|
||||||
|
.src = p.src_mac,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, ethertype_ipv4),
|
||||||
|
};
|
||||||
|
@memcpy(out[0..eth_len], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
const ip = packet.Ipv4Hdr{
|
||||||
|
.version_ihl = ipv4_version_ihl,
|
||||||
|
.tos = 0,
|
||||||
|
.total_len = std.mem.nativeToBig(u16, @intCast(ip_len + tcp_len + p.payload.len)),
|
||||||
|
.id = 0,
|
||||||
|
.flags_frag = std.mem.nativeToBig(u16, ip_flag_dont_fragment),
|
||||||
|
.ttl = default_ttl,
|
||||||
|
.protocol = ip_proto_tcp,
|
||||||
|
.checksum = 0,
|
||||||
|
.src = std.mem.nativeToBig(u32, p.src_ip),
|
||||||
|
.dst = std.mem.nativeToBig(u32, p.dst_ip),
|
||||||
|
};
|
||||||
|
@memcpy(out[eth_len..l4_off], std.mem.asBytes(&ip));
|
||||||
|
const ip_ck = packet.checksum(out[eth_len..l4_off]);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], ip_ck, .big);
|
||||||
|
|
||||||
|
const tcp = packet.TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, p.src_port),
|
||||||
|
.dst_port = std.mem.nativeToBig(u16, p.dst_port),
|
||||||
|
.seq = std.mem.nativeToBig(u32, p.seq),
|
||||||
|
.ack = std.mem.nativeToBig(u32, p.ack),
|
||||||
|
.data_off_ns = tcp_data_off_5words,
|
||||||
|
.flags = p.flags,
|
||||||
|
.window = std.mem.nativeToBig(u16, p.window),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
@memcpy(out[l4_off..payload_off], std.mem.asBytes(&tcp));
|
||||||
|
if (p.payload.len > 0) @memcpy(out[payload_off..total], p.payload);
|
||||||
|
|
||||||
|
const src_be = std.mem.nativeToBig(u32, p.src_ip);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, p.dst_ip);
|
||||||
|
const tcp_ck = packet.tcpChecksum(src_be, dst_be, out[l4_off..total]);
|
||||||
|
std.mem.writeInt(u16, out[tcp_checksum_off..][0..2], tcp_ck, .big);
|
||||||
|
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
const test_src_mac = [6]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 };
|
||||||
|
const test_dst_mac = [6]u8{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x02 };
|
||||||
|
const test_src_ip: u32 = 0x0a000001;
|
||||||
|
const test_dst_ip: u32 = 0x08080808;
|
||||||
|
|
||||||
|
fn selfVerifies(out: *[max_len]u8, n: usize) !void {
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(out[eth_len..l4_off]));
|
||||||
|
const src_be = std.mem.nativeToBig(u32, test_src_ip);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, test_dst_ip);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum(src_be, dst_be, out[l4_off..n]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a bare ACK is 54 bytes with self-verifying IP and TCP checksums" {
|
||||||
|
var out: [max_len]u8 = undefined;
|
||||||
|
const n = build(&out, .{
|
||||||
|
.src_mac = test_src_mac,
|
||||||
|
.dst_mac = test_dst_mac,
|
||||||
|
.src_ip = test_src_ip,
|
||||||
|
.dst_ip = test_dst_ip,
|
||||||
|
.src_port = 40000,
|
||||||
|
.dst_port = 22,
|
||||||
|
.seq = 0x1000_0001,
|
||||||
|
.ack = 0xCAFE_BABF,
|
||||||
|
.flags = packet.TcpFlag.ack,
|
||||||
|
});
|
||||||
|
try std.testing.expectEqual(@as(usize, 54), n);
|
||||||
|
try selfVerifies(&out, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "seq, ack, ports, and flags land at the wire offsets" {
|
||||||
|
var out: [max_len]u8 = undefined;
|
||||||
|
const n = build(&out, .{
|
||||||
|
.src_mac = test_src_mac,
|
||||||
|
.dst_mac = test_dst_mac,
|
||||||
|
.src_ip = test_src_ip,
|
||||||
|
.dst_ip = test_dst_ip,
|
||||||
|
.src_port = 40001,
|
||||||
|
.dst_port = 443,
|
||||||
|
.seq = 0xDEAD_BEEF,
|
||||||
|
.ack = 0x0102_0304,
|
||||||
|
.flags = packet.TcpFlag.rst | packet.TcpFlag.ack,
|
||||||
|
});
|
||||||
|
try std.testing.expectEqual(@as(u16, 40001), std.mem.readInt(u16, out[l4_off..][0..2], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 443), std.mem.readInt(u16, out[l4_off + 2 ..][0..2], .big));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0xDEAD_BEEF), std.mem.readInt(u32, out[l4_off + 4 ..][0..4], .big));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x0102_0304), std.mem.readInt(u32, out[l4_off + 8 ..][0..4], .big));
|
||||||
|
try std.testing.expectEqual(@as(u8, 0x50), out[l4_off + 12]);
|
||||||
|
try std.testing.expectEqual(packet.TcpFlag.rst | packet.TcpFlag.ack, out[l4_off + 13]);
|
||||||
|
try selfVerifies(&out, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a PSH-ACK with a probe payload carries the bytes and self-verifies" {
|
||||||
|
const probe = "GET / HTTP/1.0\r\n\r\n";
|
||||||
|
var out: [max_len]u8 = undefined;
|
||||||
|
const n = build(&out, .{
|
||||||
|
.src_mac = test_src_mac,
|
||||||
|
.dst_mac = test_dst_mac,
|
||||||
|
.src_ip = test_src_ip,
|
||||||
|
.dst_ip = test_dst_ip,
|
||||||
|
.src_port = 40002,
|
||||||
|
.dst_port = 80,
|
||||||
|
.seq = 0x2000_0002,
|
||||||
|
.ack = 0x3000_0003,
|
||||||
|
.flags = packet.TcpFlag.psh | packet.TcpFlag.ack,
|
||||||
|
.payload = probe,
|
||||||
|
});
|
||||||
|
try std.testing.expectEqual(@as(usize, 54 + probe.len), n);
|
||||||
|
try std.testing.expectEqualSlices(u8, probe, out[payload_off..n]);
|
||||||
|
try std.testing.expectEqual(@as(u16, @intCast(ip_len + tcp_len + probe.len)), std.mem.readInt(u16, out[eth_len + 2 ..][0..2], .big));
|
||||||
|
try selfVerifies(&out, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the ethernet header carries both MACs and the IPv4 ethertype" {
|
||||||
|
var out: [max_len]u8 = undefined;
|
||||||
|
_ = build(&out, .{
|
||||||
|
.src_mac = test_src_mac,
|
||||||
|
.dst_mac = test_dst_mac,
|
||||||
|
.src_ip = test_src_ip,
|
||||||
|
.dst_ip = test_dst_ip,
|
||||||
|
.src_port = 40000,
|
||||||
|
.dst_port = 22,
|
||||||
|
.seq = 1,
|
||||||
|
.ack = 2,
|
||||||
|
.flags = packet.TcpFlag.ack,
|
||||||
|
});
|
||||||
|
try std.testing.expectEqualSlices(u8, &test_dst_mac, out[0..6]);
|
||||||
|
try std.testing.expectEqualSlices(u8, &test_src_mac, out[6..12]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x0800), std.mem.readInt(u16, out[12..14], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(payload_off == 54);
|
||||||
|
std.debug.assert(max_len == 54 + max_payload);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,775 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// service.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const packet = @import("packet");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const segment = @import("segment");
|
||||||
|
const probe = @import("probe");
|
||||||
|
|
||||||
|
pub const default_capacity: u16 = 8192;
|
||||||
|
pub const default_banner_wait_ns: u64 = 4 * std.time.ns_per_s;
|
||||||
|
|
||||||
|
const ETH_HDR_LEN: usize = 14;
|
||||||
|
const ETH_OFF_TYPE: usize = 12;
|
||||||
|
const ETHERTYPE_IPV4: u16 = 0x0800;
|
||||||
|
const IP_MIN_IHL: usize = 20;
|
||||||
|
const IP_IHL_MASK: u8 = 0x0f;
|
||||||
|
const IP_OFF_TOTAL_LEN: usize = 2;
|
||||||
|
const IP_OFF_PROTO: usize = 9;
|
||||||
|
const IP_OFF_SRC: usize = 12;
|
||||||
|
const IP_OFF_DST: usize = 16;
|
||||||
|
const IPPROTO_TCP: u8 = 6;
|
||||||
|
|
||||||
|
const TCP_OFF_SPORT: usize = 0;
|
||||||
|
const TCP_OFF_DPORT: usize = 2;
|
||||||
|
const TCP_OFF_SEQ: usize = 4;
|
||||||
|
const TCP_OFF_ACK: usize = 8;
|
||||||
|
const TCP_OFF_DATAOFF: usize = 12;
|
||||||
|
const TCP_OFF_FLAGS: usize = 13;
|
||||||
|
const TCP_MIN_LEN: usize = 20;
|
||||||
|
|
||||||
|
fn ihlBytes(first_byte: u8) usize {
|
||||||
|
return @as(usize, first_byte & IP_IHL_MASK) * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const View = struct {
|
||||||
|
ip_src: u32,
|
||||||
|
ip_dst: u32,
|
||||||
|
sport: u16,
|
||||||
|
dport: u16,
|
||||||
|
seq: u32,
|
||||||
|
ack: u32,
|
||||||
|
flags: u8,
|
||||||
|
payload: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn parse(frame: []const u8) ?View {
|
||||||
|
if (frame.len < ETH_HDR_LEN + IP_MIN_IHL) return null;
|
||||||
|
if (std.mem.readInt(u16, frame[ETH_OFF_TYPE..][0..2], .big) != ETHERTYPE_IPV4) return null;
|
||||||
|
|
||||||
|
const ip = ETH_HDR_LEN;
|
||||||
|
const ihl = ihlBytes(frame[ip]);
|
||||||
|
if (ihl < IP_MIN_IHL or frame.len < ip + ihl) return null;
|
||||||
|
if (frame[ip + IP_OFF_PROTO] != IPPROTO_TCP) return null;
|
||||||
|
|
||||||
|
const total_len = std.mem.readInt(u16, frame[ip + IP_OFF_TOTAL_LEN ..][0..2], .big);
|
||||||
|
const ip_src = std.mem.readInt(u32, frame[ip + IP_OFF_SRC ..][0..4], .big);
|
||||||
|
const ip_dst = std.mem.readInt(u32, frame[ip + IP_OFF_DST ..][0..4], .big);
|
||||||
|
|
||||||
|
const tcp = ip + ihl;
|
||||||
|
if (frame.len < tcp + TCP_MIN_LEN) return null;
|
||||||
|
const data_off = ihlBytes(frame[tcp + TCP_OFF_DATAOFF] >> 4);
|
||||||
|
if (data_off < TCP_MIN_LEN) return null;
|
||||||
|
|
||||||
|
const payload_start = tcp + data_off;
|
||||||
|
const ip_end = @min(ip + @as(usize, total_len), frame.len);
|
||||||
|
const payload: []const u8 = if (payload_start < ip_end and payload_start <= frame.len)
|
||||||
|
frame[payload_start..@min(ip_end, frame.len)]
|
||||||
|
else
|
||||||
|
&.{};
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.ip_src = ip_src,
|
||||||
|
.ip_dst = ip_dst,
|
||||||
|
.sport = std.mem.readInt(u16, frame[tcp + TCP_OFF_SPORT ..][0..2], .big),
|
||||||
|
.dport = std.mem.readInt(u16, frame[tcp + TCP_OFF_DPORT ..][0..2], .big),
|
||||||
|
.seq = std.mem.readInt(u32, frame[tcp + TCP_OFF_SEQ ..][0..4], .big),
|
||||||
|
.ack = std.mem.readInt(u32, frame[tcp + TCP_OFF_ACK ..][0..4], .big),
|
||||||
|
.flags = frame[tcp + TCP_OFF_FLAGS],
|
||||||
|
.payload = payload,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connKey(ip: u32, port: u16) u64 {
|
||||||
|
return (@as(u64, ip) << 16) | port;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keyIp(key: u64) u32 {
|
||||||
|
return @intCast(key >> 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn keyPort(key: u64) u16 {
|
||||||
|
return @truncate(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const nil: u16 = 0xffff;
|
||||||
|
|
||||||
|
pub fn ConnTable(comptime cap: u16) type {
|
||||||
|
std.debug.assert(cap < nil);
|
||||||
|
return struct {
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
const Node = struct {
|
||||||
|
key: u64 = 0,
|
||||||
|
our_port: u16 = 0,
|
||||||
|
server_isn: u32 = 0,
|
||||||
|
deadline: u64 = 0,
|
||||||
|
next: u16 = nil,
|
||||||
|
};
|
||||||
|
|
||||||
|
buckets: [cap]u16 = [_]u16{nil} ** cap,
|
||||||
|
pool: [cap]Node = [_]Node{.{}} ** cap,
|
||||||
|
free_head: u16,
|
||||||
|
count: u16 = 0,
|
||||||
|
seed: u64 = 0,
|
||||||
|
|
||||||
|
pub fn init(seed: u64) Self {
|
||||||
|
var self = Self{ .free_head = 0, .seed = seed };
|
||||||
|
var i: u16 = 0;
|
||||||
|
while (i < cap) : (i += 1) {
|
||||||
|
self.pool[i].next = if (i + 1 < cap) i + 1 else nil;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bucketOf(self: *const Self, key: u64) usize {
|
||||||
|
const mixed = (key ^ self.seed ^ (key >> 17)) *% 0x9e3779b97f4a7c15;
|
||||||
|
return @intCast((mixed >> 40) % cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(self: *Self, key: u64) ?*Node {
|
||||||
|
var idx = self.buckets[self.bucketOf(key)];
|
||||||
|
while (idx != nil) : (idx = self.pool[idx].next) {
|
||||||
|
if (self.pool[idx].key == key) return &self.pool[idx];
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(self: *Self, key: u64, our_port: u16, server_isn: u32, deadline: u64) bool {
|
||||||
|
if (self.get(key)) |node| {
|
||||||
|
node.our_port = our_port;
|
||||||
|
node.server_isn = server_isn;
|
||||||
|
node.deadline = deadline;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (self.free_head == nil) return false;
|
||||||
|
const idx = self.free_head;
|
||||||
|
self.free_head = self.pool[idx].next;
|
||||||
|
const b = self.bucketOf(key);
|
||||||
|
self.pool[idx] = .{ .key = key, .our_port = our_port, .server_isn = server_isn, .deadline = deadline, .next = self.buckets[b] };
|
||||||
|
self.buckets[b] = idx;
|
||||||
|
self.count += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(self: *Self, key: u64) bool {
|
||||||
|
const b = self.bucketOf(key);
|
||||||
|
var idx = self.buckets[b];
|
||||||
|
var prev: u16 = nil;
|
||||||
|
while (idx != nil) {
|
||||||
|
if (self.pool[idx].key == key) {
|
||||||
|
if (prev == nil) self.buckets[b] = self.pool[idx].next else self.pool[prev].next = self.pool[idx].next;
|
||||||
|
self.pool[idx].next = self.free_head;
|
||||||
|
self.free_head = idx;
|
||||||
|
self.count -= 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
prev = idx;
|
||||||
|
idx = self.pool[idx].next;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sweepExpired(self: *Self, now: u64, ctx: anytype) void {
|
||||||
|
var b: usize = 0;
|
||||||
|
while (b < cap) : (b += 1) {
|
||||||
|
var idx = self.buckets[b];
|
||||||
|
var prev: u16 = nil;
|
||||||
|
while (idx != nil) {
|
||||||
|
const nx = self.pool[idx].next;
|
||||||
|
if (self.pool[idx].deadline <= now) {
|
||||||
|
ctx.onExpired(self.pool[idx].key, self.pool[idx].our_port);
|
||||||
|
if (prev == nil) self.buckets[b] = nx else self.pool[prev].next = nx;
|
||||||
|
self.pool[idx].next = self.free_head;
|
||||||
|
self.free_head = idx;
|
||||||
|
self.count -= 1;
|
||||||
|
} else {
|
||||||
|
prev = idx;
|
||||||
|
}
|
||||||
|
idx = nx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Finding = struct {
|
||||||
|
ip: u32,
|
||||||
|
port: u16,
|
||||||
|
info: probe.ServiceInfo,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Sender = struct {
|
||||||
|
ctx: *anyopaque,
|
||||||
|
vtable: *const Vtable,
|
||||||
|
|
||||||
|
pub const Vtable = struct {
|
||||||
|
send: *const fn (*anyopaque, []const u8) void,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn send(self: Sender, frame: []const u8) void {
|
||||||
|
self.vtable.send(self.ctx, frame);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Sink = struct {
|
||||||
|
ctx: *anyopaque,
|
||||||
|
vtable: *const Vtable,
|
||||||
|
|
||||||
|
pub const Vtable = struct {
|
||||||
|
emit: *const fn (*anyopaque, Finding) void,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn emit(self: Sink, f: Finding) void {
|
||||||
|
self.vtable.emit(self.ctx, f);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
our_ip: u32,
|
||||||
|
src_mac: [6]u8,
|
||||||
|
gw_mac: [6]u8,
|
||||||
|
banner_wait_ns: u64 = default_banner_wait_ns,
|
||||||
|
};
|
||||||
|
|
||||||
|
const F_FIN: u8 = packet.TcpFlag.fin;
|
||||||
|
const F_SYN: u8 = packet.TcpFlag.syn;
|
||||||
|
const F_RST: u8 = packet.TcpFlag.rst;
|
||||||
|
const F_PSH: u8 = packet.TcpFlag.psh;
|
||||||
|
const F_ACK: u8 = packet.TcpFlag.ack;
|
||||||
|
|
||||||
|
pub const Engine = struct {
|
||||||
|
const Table = ConnTable(default_capacity);
|
||||||
|
|
||||||
|
cfg: Config,
|
||||||
|
table: Table,
|
||||||
|
banners: std.atomic.Value(u64) = .{ .raw = 0 },
|
||||||
|
probed: std.atomic.Value(u64) = .{ .raw = 0 },
|
||||||
|
drops: std.atomic.Value(u64) = .{ .raw = 0 },
|
||||||
|
|
||||||
|
pub fn init(cfg: Config) Engine {
|
||||||
|
const seed = cfg.cookie.generate(0, 0, 0, 0);
|
||||||
|
return .{ .cfg = cfg, .table = Table.init(seed) };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sendSegment(self: *Engine, sender: Sender, dst_ip: u32, dst_port: u16, src_port: u16, seq: u32, ack: u32, flags: u8, payload: []const u8) void {
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
const n = segment.build(&buf, .{
|
||||||
|
.src_mac = self.cfg.src_mac,
|
||||||
|
.dst_mac = self.cfg.gw_mac,
|
||||||
|
.src_ip = self.cfg.our_ip,
|
||||||
|
.dst_ip = dst_ip,
|
||||||
|
.src_port = src_port,
|
||||||
|
.dst_port = dst_port,
|
||||||
|
.seq = seq,
|
||||||
|
.ack = ack,
|
||||||
|
.flags = flags,
|
||||||
|
.payload = payload,
|
||||||
|
});
|
||||||
|
sender.send(buf[0..n]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn onSynAck(self: *Engine, v: View, key: u64, now_ns: u64, sender: Sender) void {
|
||||||
|
const our_port = v.dport;
|
||||||
|
const c = self.cfg.cookie.seq(v.ip_src, v.sport, self.cfg.our_ip, our_port);
|
||||||
|
if (v.ack != c +% 1) return;
|
||||||
|
|
||||||
|
if (self.table.get(key) == null) {
|
||||||
|
if (!self.table.insert(key, our_port, v.seq, now_ns +% self.cfg.banner_wait_ns)) {
|
||||||
|
_ = self.drops.fetchAdd(1, .monotonic);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_ = self.probed.fetchAdd(1, .monotonic);
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = probe.probeFor(v.sport);
|
||||||
|
const flags: u8 = if (payload.len > 0) F_PSH | F_ACK else F_ACK;
|
||||||
|
self.sendSegment(sender, v.ip_src, v.sport, our_port, c +% 1, v.seq +% 1, flags, payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn onData(self: *Engine, v: View, key: u64, sender: Sender, sink: Sink) void {
|
||||||
|
const node = self.table.get(key) orelse return;
|
||||||
|
if (v.dport != node.our_port) return;
|
||||||
|
if (v.seq != node.server_isn +% 1) return;
|
||||||
|
const our_port = node.our_port;
|
||||||
|
|
||||||
|
var si: probe.ServiceInfo = .{};
|
||||||
|
probe.classify(v.payload, &si);
|
||||||
|
sink.emit(.{ .ip = v.ip_src, .port = v.sport, .info = si });
|
||||||
|
_ = self.banners.fetchAdd(1, .monotonic);
|
||||||
|
|
||||||
|
const c = self.cfg.cookie.seq(v.ip_src, v.sport, self.cfg.our_ip, our_port);
|
||||||
|
const probe_len: u32 = @intCast(probe.probeFor(v.sport).len);
|
||||||
|
const our_seq = c +% 1 +% probe_len;
|
||||||
|
const their_next = v.seq +% @as(u32, @intCast(v.payload.len));
|
||||||
|
self.sendSegment(sender, v.ip_src, v.sport, our_port, our_seq, their_next, F_RST | F_ACK, "");
|
||||||
|
_ = self.table.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn onFrame(self: *Engine, frame: []const u8, now_ns: u64, sender: Sender, sink: Sink) void {
|
||||||
|
const v = parse(frame) orelse return;
|
||||||
|
if (v.ip_dst != self.cfg.our_ip) return;
|
||||||
|
const key = connKey(v.ip_src, v.sport);
|
||||||
|
|
||||||
|
const syn = (v.flags & F_SYN) != 0;
|
||||||
|
const ack = (v.flags & F_ACK) != 0;
|
||||||
|
const rst = (v.flags & F_RST) != 0;
|
||||||
|
const fin = (v.flags & F_FIN) != 0;
|
||||||
|
|
||||||
|
if (syn and ack and !rst) {
|
||||||
|
self.onSynAck(v, key, now_ns, sender);
|
||||||
|
} else if (ack and !syn and !rst and v.payload.len > 0) {
|
||||||
|
self.onData(v, key, sender, sink);
|
||||||
|
} else if (rst or fin) {
|
||||||
|
_ = self.table.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const SweepCtx = struct {
|
||||||
|
engine: *Engine,
|
||||||
|
sender: Sender,
|
||||||
|
|
||||||
|
fn onExpired(self: *SweepCtx, key: u64, our_port: u16) void {
|
||||||
|
const eng = self.engine;
|
||||||
|
const ip = keyIp(key);
|
||||||
|
const port = keyPort(key);
|
||||||
|
const c = eng.cfg.cookie.seq(ip, port, eng.cfg.our_ip, our_port);
|
||||||
|
const probe_len: u32 = @intCast(probe.probeFor(port).len);
|
||||||
|
eng.sendSegment(self.sender, ip, port, our_port, c +% 1 +% probe_len, 0, F_RST, "");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn sweep(self: *Engine, now_ns: u64, sender: Sender) void {
|
||||||
|
var ctx = SweepCtx{ .engine = self, .sender = sender };
|
||||||
|
self.table.sweepExpired(now_ns, &ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flushAll(self: *Engine, sender: Sender) void {
|
||||||
|
self.sweep(std.math.maxInt(u64), sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn inFlight(self: *const Engine) u16 {
|
||||||
|
return self.table.count;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const OpenError = error{
|
||||||
|
NeedCapNetRaw,
|
||||||
|
SocketFailed,
|
||||||
|
IfIndexFailed,
|
||||||
|
BindFailed,
|
||||||
|
};
|
||||||
|
|
||||||
|
const POLL_TICK_MS: i32 = 100;
|
||||||
|
|
||||||
|
pub const Socket = struct {
|
||||||
|
fd: i32,
|
||||||
|
sll: linux.sockaddr.ll,
|
||||||
|
|
||||||
|
pub fn open(ifname: []const u8) OpenError!Socket {
|
||||||
|
const rc_sock = linux.socket(
|
||||||
|
linux.AF.PACKET,
|
||||||
|
linux.SOCK.RAW,
|
||||||
|
std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP)),
|
||||||
|
);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => return error.NeedCapNetRaw,
|
||||||
|
else => return error.SocketFailed,
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
errdefer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) return error.IfIndexFailed;
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS)
|
||||||
|
return error.IfIndexFailed;
|
||||||
|
const ifindex: i32 = ifr.ifru.ivalue;
|
||||||
|
|
||||||
|
var ignore_outgoing: u32 = 1;
|
||||||
|
_ = linux.setsockopt(fd, linux.SOL.PACKET, linux.PACKET.IGNORE_OUTGOING, std.mem.asBytes(&ignore_outgoing), @sizeOf(u32));
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP));
|
||||||
|
sll.ifindex = ifindex;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS)
|
||||||
|
return error.BindFailed;
|
||||||
|
|
||||||
|
return .{ .fd = fd, .sll = sll };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn recv(self: *Socket, buf: []u8, timeout_ms: i32) ?usize {
|
||||||
|
var pfd = [_]linux.pollfd{.{ .fd = self.fd, .events = linux.POLL.IN, .revents = 0 }};
|
||||||
|
const pr = linux.poll(&pfd, 1, timeout_ms);
|
||||||
|
switch (linux.errno(pr)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
if (pr == 0) return null;
|
||||||
|
const rc = linux.recvfrom(self.fd, buf.ptr, buf.len, 0, null, null);
|
||||||
|
switch (linux.errno(rc)) {
|
||||||
|
.SUCCESS => return @intCast(rc),
|
||||||
|
else => return null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sendImpl(ctx: *anyopaque, frame: []const u8) void {
|
||||||
|
const self: *Socket = @ptrCast(@alignCast(ctx));
|
||||||
|
_ = linux.sendto(self.fd, frame.ptr, frame.len, 0, @ptrCast(&self.sll), @sizeOf(linux.sockaddr.ll));
|
||||||
|
}
|
||||||
|
|
||||||
|
const send_vtable = Sender.Vtable{ .send = sendImpl };
|
||||||
|
|
||||||
|
pub fn sender(self: *Socket) Sender {
|
||||||
|
return .{ .ctx = self, .vtable = &send_vtable };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn close(self: *Socket) void {
|
||||||
|
_ = linux.close(self.fd);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn monoNow() u64 {
|
||||||
|
var ts: linux.timespec = undefined;
|
||||||
|
_ = linux.clock_gettime(.MONOTONIC, &ts);
|
||||||
|
return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
|
||||||
|
}
|
||||||
|
|
||||||
|
const RECV_BUF_LEN: usize = 2048;
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
engine: *Engine,
|
||||||
|
socket: *Socket,
|
||||||
|
sink: Sink,
|
||||||
|
tx_done: *std.atomic.Value(bool),
|
||||||
|
drain_window_ns: u64,
|
||||||
|
hard_cap_ns: u64,
|
||||||
|
) void {
|
||||||
|
const sender = socket.sender();
|
||||||
|
var buf: [RECV_BUF_LEN]u8 = undefined;
|
||||||
|
const start = monoNow();
|
||||||
|
const hard_deadline = start +| hard_cap_ns;
|
||||||
|
var drain_anchor: ?u64 = null;
|
||||||
|
var last_sweep: u64 = start;
|
||||||
|
const sweep_interval_ns: u64 = 50 * std.time.ns_per_ms;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
var now = monoNow();
|
||||||
|
if (now >= hard_deadline) break;
|
||||||
|
if (tx_done.load(.acquire)) {
|
||||||
|
if (drain_anchor == null) drain_anchor = now;
|
||||||
|
const past_window = now >= drain_anchor.? + drain_window_ns;
|
||||||
|
if (past_window and engine.inFlight() == 0) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (socket.recv(&buf, POLL_TICK_MS)) |n| {
|
||||||
|
now = monoNow();
|
||||||
|
engine.onFrame(buf[0..n], now, sender, sink);
|
||||||
|
}
|
||||||
|
const sweep_now = monoNow();
|
||||||
|
if (sweep_now -| last_sweep >= sweep_interval_ns) {
|
||||||
|
engine.sweep(sweep_now, sender);
|
||||||
|
last_sweep = sweep_now;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.flushAll(sender);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- tests ----
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const our_ip: u32 = 0x0a000001;
|
||||||
|
const our_mac = [6]u8{ 0x02, 0, 0, 0, 0, 0x01 };
|
||||||
|
const gw_mac = [6]u8{ 0x02, 0, 0, 0, 0, 0x02 };
|
||||||
|
const server_ip: u32 = 0xac200002;
|
||||||
|
|
||||||
|
fn testCfg() Config {
|
||||||
|
return .{ .cookie = cookie.Cookie.init(test_key), .our_ip = our_ip, .src_mac = our_mac, .gw_mac = gw_mac };
|
||||||
|
}
|
||||||
|
|
||||||
|
const FakeSender = struct {
|
||||||
|
frames: std.ArrayList([]u8),
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
|
||||||
|
fn init(allocator: std.mem.Allocator) FakeSender {
|
||||||
|
return .{ .frames = .empty, .allocator = allocator };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deinit(self: *FakeSender) void {
|
||||||
|
for (self.frames.items) |f| self.allocator.free(f);
|
||||||
|
self.frames.deinit(self.allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sendImpl(ctx: *anyopaque, frame: []const u8) void {
|
||||||
|
const self: *FakeSender = @ptrCast(@alignCast(ctx));
|
||||||
|
const copy = self.allocator.dupe(u8, frame) catch return;
|
||||||
|
self.frames.append(self.allocator, copy) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const vtable = Sender.Vtable{ .send = sendImpl };
|
||||||
|
|
||||||
|
fn sender(self: *FakeSender) Sender {
|
||||||
|
return .{ .ctx = self, .vtable = &vtable };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const FakeSink = struct {
|
||||||
|
findings: std.ArrayList(Finding),
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
|
||||||
|
fn init(allocator: std.mem.Allocator) FakeSink {
|
||||||
|
return .{ .findings = .empty, .allocator = allocator };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deinit(self: *FakeSink) void {
|
||||||
|
self.findings.deinit(self.allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn emitImpl(ctx: *anyopaque, f: Finding) void {
|
||||||
|
const self: *FakeSink = @ptrCast(@alignCast(ctx));
|
||||||
|
self.findings.append(self.allocator, f) catch {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const vtable = Sink.Vtable{ .emit = emitImpl };
|
||||||
|
|
||||||
|
fn sink(self: *FakeSink) Sink {
|
||||||
|
return .{ .ctx = self, .vtable = &vtable };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fn buildServerFrame(buf: *[segment.max_len]u8, sport: u16, dport: u16, seq: u32, ack: u32, flags: u8, payload: []const u8) []const u8 {
|
||||||
|
const n = segment.build(buf, .{
|
||||||
|
.src_mac = gw_mac,
|
||||||
|
.dst_mac = our_mac,
|
||||||
|
.src_ip = server_ip,
|
||||||
|
.dst_ip = our_ip,
|
||||||
|
.src_port = sport,
|
||||||
|
.dst_port = dport,
|
||||||
|
.seq = seq,
|
||||||
|
.ack = ack,
|
||||||
|
.flags = flags,
|
||||||
|
.payload = payload,
|
||||||
|
});
|
||||||
|
return buf[0..n];
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ConnTable inserts, finds, removes, and reports full" {
|
||||||
|
var t = ConnTable(4).init(0);
|
||||||
|
try std.testing.expect(t.insert(connKey(1, 10), 40000, 0x1000, 100));
|
||||||
|
try std.testing.expect(t.insert(connKey(2, 20), 40001, 0x2000, 200));
|
||||||
|
try std.testing.expect(t.insert(connKey(3, 30), 40002, 0x3000, 300));
|
||||||
|
try std.testing.expect(t.insert(connKey(4, 40), 40003, 0x4000, 400));
|
||||||
|
try std.testing.expect(!t.insert(connKey(5, 50), 40004, 0x5000, 500));
|
||||||
|
|
||||||
|
try std.testing.expect(t.get(connKey(2, 20)) != null);
|
||||||
|
try std.testing.expectEqual(@as(u16, 40001), t.get(connKey(2, 20)).?.our_port);
|
||||||
|
try std.testing.expect(t.remove(connKey(2, 20)));
|
||||||
|
try std.testing.expect(t.get(connKey(2, 20)) == null);
|
||||||
|
try std.testing.expect(t.insert(connKey(5, 50), 40004, 0x5000, 500));
|
||||||
|
try std.testing.expectEqual(@as(u16, 4), t.count);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CountCtx = struct {
|
||||||
|
n: usize = 0,
|
||||||
|
fn onExpired(self: *CountCtx, key: u64, our_port: u16) void {
|
||||||
|
_ = key;
|
||||||
|
_ = our_port;
|
||||||
|
self.n += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "ConnTable sweepExpired removes only entries past their deadline" {
|
||||||
|
var t = ConnTable(8).init(0);
|
||||||
|
_ = t.insert(connKey(1, 10), 40000, 0x1000, 100);
|
||||||
|
_ = t.insert(connKey(2, 20), 40001, 0x2000, 500);
|
||||||
|
_ = t.insert(connKey(3, 30), 40002, 0x3000, 100);
|
||||||
|
var ctx = CountCtx{};
|
||||||
|
t.sweepExpired(300, &ctx);
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), ctx.n);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1), t.count);
|
||||||
|
try std.testing.expect(t.get(connKey(2, 20)) != null);
|
||||||
|
try std.testing.expect(t.get(connKey(1, 10)) == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a valid SYN-ACK completes the handshake with an ACK and tracks the connection" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const c = ck.seq(server_ip, 22, our_ip, 40000);
|
||||||
|
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
const synack = buildServerFrame(&buf, 22, 40000, 0xCAFE0000, c +% 1, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), fs.frames.items.len);
|
||||||
|
const ackv = parse(fs.frames.items[0]).?;
|
||||||
|
try std.testing.expectEqual(@as(u32, c +% 1), ackv.seq);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0xCAFE0000 +% 1), ackv.ack);
|
||||||
|
try std.testing.expect((ackv.flags & F_ACK) != 0 and (ackv.flags & F_SYN) == 0);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1), eng.inFlight());
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), eng.probed.load(.monotonic));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a SYN-ACK with a forged ack is rejected and never tracked" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
const synack = buildServerFrame(&buf, 22, 40000, 0xCAFE0000, 0xDEADBEEF, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), fs.frames.items.len);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), eng.inFlight());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a data segment on a tracked connection yields a finding and a closing RST" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const c = ck.seq(server_ip, 22, our_ip, 40000);
|
||||||
|
|
||||||
|
var b1: [segment.max_len]u8 = undefined;
|
||||||
|
const synack = buildServerFrame(&b1, 22, 40000, 0xCAFE0000, c +% 1, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
var b2: [segment.max_len]u8 = undefined;
|
||||||
|
const banner = "SSH-2.0-OpenSSH_9.6p1\r\n";
|
||||||
|
const data = buildServerFrame(&b2, 22, 40000, 0xCAFE0001, c +% 1, F_PSH | F_ACK, banner);
|
||||||
|
eng.onFrame(data, 1100, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), fk.findings.items.len);
|
||||||
|
const f = fk.findings.items[0];
|
||||||
|
try std.testing.expectEqual(server_ip, f.ip);
|
||||||
|
try std.testing.expectEqual(@as(u16, 22), f.port);
|
||||||
|
try std.testing.expectEqualStrings("ssh", f.info.service);
|
||||||
|
try std.testing.expectEqualStrings("OpenSSH_9.6p1", f.info.infoSlice());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 2), fs.frames.items.len);
|
||||||
|
const rstv = parse(fs.frames.items[1]).?;
|
||||||
|
try std.testing.expect((rstv.flags & F_RST) != 0);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), eng.inFlight());
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), eng.banners.load(.monotonic));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a data segment with a forged sequence number is rejected (anti-injection)" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const c = ck.seq(server_ip, 22, our_ip, 40000);
|
||||||
|
|
||||||
|
var b1: [segment.max_len]u8 = undefined;
|
||||||
|
const synack = buildServerFrame(&b1, 22, 40000, 0xCAFE0000, c +% 1, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
var b2: [segment.max_len]u8 = undefined;
|
||||||
|
const forged = buildServerFrame(&b2, 22, 40000, 0xDEADBEEF, c +% 1, F_PSH | F_ACK, "SSH-2.0-EVIL_INJECTED\r\n");
|
||||||
|
eng.onFrame(forged, 1100, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), fk.findings.items.len);
|
||||||
|
try std.testing.expectEqual(@as(u16, 1), eng.inFlight());
|
||||||
|
|
||||||
|
var b3: [segment.max_len]u8 = undefined;
|
||||||
|
const genuine = buildServerFrame(&b3, 22, 40000, 0xCAFE0001, c +% 1, F_PSH | F_ACK, "SSH-2.0-RealBanner\r\n");
|
||||||
|
eng.onFrame(genuine, 1200, fs.sender(), fk.sink());
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), fk.findings.items.len);
|
||||||
|
try std.testing.expectEqualStrings("ssh", fk.findings.items[0].info.service);
|
||||||
|
try std.testing.expectEqualStrings("RealBanner", fk.findings.items[0].info.infoSlice());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a data segment for an untracked connection is ignored" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
const data = buildServerFrame(&buf, 22, 40000, 0xCAFE0001, 0x1234, F_PSH | F_ACK, "hello");
|
||||||
|
eng.onFrame(data, 1100, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), fk.findings.items.len);
|
||||||
|
try std.testing.expectEqual(@as(usize, 0), fs.frames.items.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an HTTP port sends a GET probe on the completing ACK" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const c = ck.seq(server_ip, 80, our_ip, 40000);
|
||||||
|
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
const synack = buildServerFrame(&buf, 80, 40000, 0x1000, c +% 1, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
|
||||||
|
const ackv = parse(fs.frames.items[0]).?;
|
||||||
|
try std.testing.expect((ackv.flags & F_PSH) != 0);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, ackv.payload, "GET /") != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an expired connection is swept with a RST and the drop counter tracks backpressure" {
|
||||||
|
var fs = FakeSender.init(std.testing.allocator);
|
||||||
|
defer fs.deinit();
|
||||||
|
|
||||||
|
var eng = Engine.init(testCfg());
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const c = ck.seq(server_ip, 22, our_ip, 40000);
|
||||||
|
var buf: [segment.max_len]u8 = undefined;
|
||||||
|
var fk = FakeSink.init(std.testing.allocator);
|
||||||
|
defer fk.deinit();
|
||||||
|
const synack = buildServerFrame(&buf, 22, 40000, 0xCAFE0000, c +% 1, F_SYN | F_ACK, "");
|
||||||
|
eng.onFrame(synack, 1000, fs.sender(), fk.sink());
|
||||||
|
try std.testing.expectEqual(@as(u16, 1), eng.inFlight());
|
||||||
|
|
||||||
|
eng.sweep(1000 + default_banner_wait_ns + 1, fs.sender());
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), eng.inFlight());
|
||||||
|
const rstv = parse(fs.frames.items[fs.frames.items.len - 1]).?;
|
||||||
|
try std.testing.expect((rstv.flags & F_RST) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parse strips ethernet padding using the IP total-length field" {
|
||||||
|
var frame = [_]u8{0} ** 64;
|
||||||
|
std.mem.writeInt(u16, frame[ETH_OFF_TYPE..][0..2], ETHERTYPE_IPV4, .big);
|
||||||
|
frame[ETH_HDR_LEN] = 0x45;
|
||||||
|
const total_len: u16 = 20 + 20 + 3;
|
||||||
|
std.mem.writeInt(u16, frame[ETH_HDR_LEN + IP_OFF_TOTAL_LEN ..][0..2], total_len, .big);
|
||||||
|
frame[ETH_HDR_LEN + IP_OFF_PROTO] = IPPROTO_TCP;
|
||||||
|
std.mem.writeInt(u32, frame[ETH_HDR_LEN + IP_OFF_SRC ..][0..4], server_ip, .big);
|
||||||
|
std.mem.writeInt(u32, frame[ETH_HDR_LEN + IP_OFF_DST ..][0..4], our_ip, .big);
|
||||||
|
const tcp = ETH_HDR_LEN + IP_MIN_IHL;
|
||||||
|
frame[tcp + TCP_OFF_DATAOFF] = 0x50;
|
||||||
|
frame[tcp + TCP_MIN_LEN] = 'a';
|
||||||
|
frame[tcp + TCP_MIN_LEN + 1] = 'b';
|
||||||
|
frame[tcp + TCP_MIN_LEN + 2] = 'c';
|
||||||
|
|
||||||
|
const v = parse(&frame).?;
|
||||||
|
try std.testing.expectEqualStrings("abc", v.payload);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// smoke.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
fn build_syn(dst_port: u16) [54]u8 {
|
||||||
|
var frame: [54]u8 = undefined;
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = .{0} ** 6,
|
||||||
|
.src = .{0} ** 6,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, 0x0800),
|
||||||
|
};
|
||||||
|
@memcpy(frame[0..14], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
var ip = packet.Ipv4Hdr{
|
||||||
|
.version_ihl = 0x45,
|
||||||
|
.tos = 0,
|
||||||
|
.total_len = std.mem.nativeToBig(u16, 40),
|
||||||
|
.id = 0,
|
||||||
|
.flags_frag = std.mem.nativeToBig(u16, 0x4000),
|
||||||
|
.ttl = 64,
|
||||||
|
.protocol = 6,
|
||||||
|
.checksum = 0,
|
||||||
|
.src = std.mem.nativeToBig(u32, 0x7f000001),
|
||||||
|
.dst = std.mem.nativeToBig(u32, 0x7f000001),
|
||||||
|
};
|
||||||
|
ip.checksum = std.mem.nativeToBig(u16, packet.checksum(std.mem.asBytes(&ip)));
|
||||||
|
@memcpy(frame[14..34], std.mem.asBytes(&ip));
|
||||||
|
|
||||||
|
var tcp = packet.TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, 54321),
|
||||||
|
.dst_port = std.mem.nativeToBig(u16, dst_port),
|
||||||
|
.seq = std.mem.nativeToBig(u32, 0xdead_beef),
|
||||||
|
.ack = 0,
|
||||||
|
.data_off_ns = 0x50,
|
||||||
|
.flags = 0x02,
|
||||||
|
.window = std.mem.nativeToBig(u16, 1024),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
tcp.checksum = std.mem.nativeToBig(u16, packet.tcpChecksum(ip.src, ip.dst, std.mem.asBytes(&tcp)));
|
||||||
|
@memcpy(frame[34..54], std.mem.asBytes(&tcp));
|
||||||
|
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(io: std.Io, ifname: []const u8) !void {
|
||||||
|
var buf: [256]u8 = undefined;
|
||||||
|
var fw = std.Io.File.stdout().writer(io, &buf);
|
||||||
|
const out = &fw.interface;
|
||||||
|
|
||||||
|
const rc_sock = linux.socket(
|
||||||
|
linux.AF.PACKET,
|
||||||
|
linux.SOCK.RAW,
|
||||||
|
std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP)),
|
||||||
|
);
|
||||||
|
switch (linux.errno(rc_sock)) {
|
||||||
|
.SUCCESS => {},
|
||||||
|
.PERM, .ACCES => {
|
||||||
|
try out.writeAll("smoke: need CAP_NET_RAW. Grant it once, then re-run (no sudo):\n sudo setcap cap_net_raw,cap_net_admin=eip ./zig-out/bin/zingela\n ./zig-out/bin/zingela smoke\nSkipping.\n");
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => |e| {
|
||||||
|
try out.print("smoke: socket() failed: {s}\n", .{@tagName(e)});
|
||||||
|
try out.flush();
|
||||||
|
return error.SocketFailed;
|
||||||
|
},
|
||||||
|
}
|
||||||
|
const fd: i32 = @intCast(rc_sock);
|
||||||
|
defer _ = linux.close(fd);
|
||||||
|
|
||||||
|
var ifr = std.mem.zeroes(linux.ifreq);
|
||||||
|
if (ifname.len >= ifr.ifrn.name.len) {
|
||||||
|
try out.print("smoke: interface name too long: {s}\n", .{ifname});
|
||||||
|
try out.flush();
|
||||||
|
return error.IfNameTooLong;
|
||||||
|
}
|
||||||
|
@memcpy(ifr.ifrn.name[0..ifname.len], ifname);
|
||||||
|
if (linux.errno(linux.ioctl(fd, linux.SIOCGIFINDEX, @intFromPtr(&ifr))) != .SUCCESS) {
|
||||||
|
try out.print("smoke: SIOCGIFINDEX failed for {s}\n", .{ifname});
|
||||||
|
try out.flush();
|
||||||
|
return error.IfIndexFailed;
|
||||||
|
}
|
||||||
|
const ifindex: i32 = ifr.ifru.ivalue;
|
||||||
|
|
||||||
|
var sll = std.mem.zeroes(linux.sockaddr.ll);
|
||||||
|
sll.family = linux.AF.PACKET;
|
||||||
|
sll.protocol = std.mem.nativeToBig(u16, @as(u16, linux.ETH.P.IP));
|
||||||
|
sll.ifindex = ifindex;
|
||||||
|
if (linux.errno(linux.bind(fd, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll))) != .SUCCESS) {
|
||||||
|
try out.writeAll("smoke: bind() failed\n");
|
||||||
|
try out.flush();
|
||||||
|
return error.BindFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
const frame = build_syn(80);
|
||||||
|
const rc_send = linux.sendto(fd, &frame, frame.len, 0, @ptrCast(&sll), @sizeOf(linux.sockaddr.ll));
|
||||||
|
if (linux.errno(rc_send) != .SUCCESS) {
|
||||||
|
try out.writeAll("smoke: sendto() failed\n");
|
||||||
|
try out.flush();
|
||||||
|
return error.SendFailed;
|
||||||
|
}
|
||||||
|
|
||||||
|
try out.print("smoke: OK. Sent {d} bytes (one SYN -> 127.0.0.1:80) on {s} via AF_PACKET.\n", .{ rc_send, ifname });
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
test "built SYN frame is 54 bytes and IP checksum self-verifies" {
|
||||||
|
const frame = build_syn(80);
|
||||||
|
try std.testing.expectEqual(@as(usize, 54), frame.len);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(frame[14..34]));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,341 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// stealth.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
const packet = @import("packet");
|
||||||
|
const netutil = @import("netutil");
|
||||||
|
|
||||||
|
pub const default_rotate_span: u16 = 8192;
|
||||||
|
pub const max_decoys: usize = 16;
|
||||||
|
pub const random_ip_attempts: usize = 64;
|
||||||
|
|
||||||
|
const routable_fallback_ip: u32 = 0x01010101;
|
||||||
|
|
||||||
|
pub const ParseError = error{
|
||||||
|
AuthorizationRequired,
|
||||||
|
BadOsTemplate,
|
||||||
|
BadScanType,
|
||||||
|
BadJitterMode,
|
||||||
|
BadDecoySpec,
|
||||||
|
TooManyDecoys,
|
||||||
|
OutOfMemory,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const RstError = error{
|
||||||
|
IptablesSpawnFailed,
|
||||||
|
IptablesFailed,
|
||||||
|
OutOfMemory,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
authorized: bool = false,
|
||||||
|
profile: packet.OsProfile = .none,
|
||||||
|
scan: packet.ScanType = .syn,
|
||||||
|
jitter: bool = false,
|
||||||
|
rotate: bool = false,
|
||||||
|
rotate_span: u16 = default_rotate_span,
|
||||||
|
suppress_rst: bool = false,
|
||||||
|
decoys: []const u32 = &.{},
|
||||||
|
|
||||||
|
pub fn deinit(self: *Config, allocator: std.mem.Allocator) void {
|
||||||
|
if (self.decoys.len > 0) allocator.free(self.decoys);
|
||||||
|
self.decoys = &.{};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn parse(allocator: std.mem.Allocator, io: std.Io, args: []const []const u8) ParseError!Config {
|
||||||
|
const os_flag = netutil.getFlag(args, "--os-template");
|
||||||
|
const scan_flag = netutil.getFlag(args, "--scan-type");
|
||||||
|
const jitter_flag = netutil.getFlag(args, "--jitter");
|
||||||
|
const rotate_flag = netutil.hasFlag(args, "--source-port-rotation");
|
||||||
|
const decoy_flag = netutil.getFlag(args, "--decoys");
|
||||||
|
const suppress_flag = netutil.hasFlag(args, "--suppress-rst");
|
||||||
|
|
||||||
|
const requested = os_flag != null or scan_flag != null or jitter_flag != null or
|
||||||
|
rotate_flag or decoy_flag != null or suppress_flag;
|
||||||
|
|
||||||
|
if (!requested) return .{};
|
||||||
|
if (!netutil.hasFlag(args, "--authorized-scan")) return error.AuthorizationRequired;
|
||||||
|
|
||||||
|
var cfg = Config{ .authorized = true, .rotate = rotate_flag, .suppress_rst = suppress_flag };
|
||||||
|
errdefer cfg.deinit(allocator);
|
||||||
|
|
||||||
|
if (os_flag) |t| cfg.profile = packet.OsProfile.parse(t) orelse return error.BadOsTemplate;
|
||||||
|
if (scan_flag) |t| cfg.scan = packet.ScanType.parse(t) orelse return error.BadScanType;
|
||||||
|
if (jitter_flag) |t| {
|
||||||
|
if (std.mem.eql(u8, t, "poisson")) {
|
||||||
|
cfg.jitter = true;
|
||||||
|
} else if (std.mem.eql(u8, t, "none")) {
|
||||||
|
cfg.jitter = false;
|
||||||
|
} else return error.BadJitterMode;
|
||||||
|
}
|
||||||
|
if (decoy_flag) |spec| cfg.decoys = try parseDecoys(allocator, io, spec);
|
||||||
|
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseDecoys(allocator: std.mem.Allocator, io: std.Io, spec: []const u8) ParseError![]const u32 {
|
||||||
|
var list: std.ArrayList(u32) = .empty;
|
||||||
|
errdefer list.deinit(allocator);
|
||||||
|
|
||||||
|
var it = std.mem.splitScalar(u8, spec, ',');
|
||||||
|
while (it.next()) |tok| {
|
||||||
|
if (tok.len == 0) continue;
|
||||||
|
if (list.items.len >= max_decoys) return error.TooManyDecoys;
|
||||||
|
if (std.mem.startsWith(u8, tok, "RND:")) {
|
||||||
|
const n = std.fmt.parseInt(usize, tok[4..], 10) catch return error.BadDecoySpec;
|
||||||
|
var made: usize = 0;
|
||||||
|
while (made < n) : (made += 1) {
|
||||||
|
if (list.items.len >= max_decoys) return error.TooManyDecoys;
|
||||||
|
try list.append(allocator, randomNonBogon(io));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const ip = netutil.parseIpv4(tok) catch return error.BadDecoySpec;
|
||||||
|
try list.append(allocator, ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (list.items.len == 0) return error.BadDecoySpec;
|
||||||
|
return list.toOwnedSlice(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn randomNonBogon(io: std.Io) u32 {
|
||||||
|
var attempts: usize = 0;
|
||||||
|
while (attempts < random_ip_attempts) : (attempts += 1) {
|
||||||
|
var b: [4]u8 = undefined;
|
||||||
|
io.randomSecure(&b) catch continue;
|
||||||
|
const ip = std.mem.readInt(u32, &b, .big);
|
||||||
|
if (!isBogonV4(ip)) return ip;
|
||||||
|
}
|
||||||
|
return routable_fallback_ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn inNet(ip: u32, net: u32, bits: u5) bool {
|
||||||
|
const sh: u5 = @intCast(32 - @as(u32, bits));
|
||||||
|
const mask: u32 = ~@as(u32, 0) << sh;
|
||||||
|
return (ip & mask) == (net & mask);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isBogonV4(ip: u32) bool {
|
||||||
|
if (ip >> 28 == 0xE) return true;
|
||||||
|
if (ip >> 28 == 0xF) return true;
|
||||||
|
return inNet(ip, 0x00000000, 8) or
|
||||||
|
inNet(ip, 0x0A000000, 8) or
|
||||||
|
inNet(ip, 0x64400000, 10) or
|
||||||
|
inNet(ip, 0x7F000000, 8) or
|
||||||
|
inNet(ip, 0xA9FE0000, 16) or
|
||||||
|
inNet(ip, 0xAC100000, 12) or
|
||||||
|
inNet(ip, 0xC0000000, 24) or
|
||||||
|
inNet(ip, 0xC0000200, 24) or
|
||||||
|
inNet(ip, 0xC0586300, 24) or
|
||||||
|
inNet(ip, 0xC0A80000, 16) or
|
||||||
|
inNet(ip, 0xC6120000, 15) or
|
||||||
|
inNet(ip, 0xC6336400, 24) or
|
||||||
|
inNet(ip, 0xCB007100, 24);
|
||||||
|
}
|
||||||
|
|
||||||
|
const pr_cap_ambient: i32 = 47;
|
||||||
|
const pr_cap_ambient_raise: usize = 2;
|
||||||
|
const cap_net_admin: usize = 12;
|
||||||
|
|
||||||
|
fn raiseAmbientNetAdmin() void {
|
||||||
|
_ = linux.prctl(pr_cap_ambient, pr_cap_ambient_raise, cap_net_admin, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn ipToStr(buf: *[15]u8, ip: u32) []const u8 {
|
||||||
|
return std.fmt.bufPrint(buf, "{d}.{d}.{d}.{d}", .{
|
||||||
|
(ip >> 24) & 0xff,
|
||||||
|
(ip >> 16) & 0xff,
|
||||||
|
(ip >> 8) & 0xff,
|
||||||
|
ip & 0xff,
|
||||||
|
}) catch unreachable;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const RstSuppressor = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
io: std.Io,
|
||||||
|
ip_str: []u8,
|
||||||
|
range_str: []u8,
|
||||||
|
installed: bool,
|
||||||
|
|
||||||
|
pub fn install(allocator: std.mem.Allocator, io: std.Io, src_ip: u32, lo: u16, hi: u16) RstError!RstSuppressor {
|
||||||
|
var ipbuf: [15]u8 = undefined;
|
||||||
|
const ip_str = try allocator.dupe(u8, ipToStr(&ipbuf, src_ip));
|
||||||
|
errdefer allocator.free(ip_str);
|
||||||
|
const range_str = try std.fmt.allocPrint(allocator, "{d}:{d}", .{ lo, hi });
|
||||||
|
errdefer allocator.free(range_str);
|
||||||
|
|
||||||
|
var self = RstSuppressor{
|
||||||
|
.allocator = allocator,
|
||||||
|
.io = io,
|
||||||
|
.ip_str = ip_str,
|
||||||
|
.range_str = range_str,
|
||||||
|
.installed = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
raiseAmbientNetAdmin();
|
||||||
|
self.runIptables("-D") catch {};
|
||||||
|
try self.runIptables("-I");
|
||||||
|
self.installed = true;
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runIptables(self: *RstSuppressor, action: []const u8) RstError!void {
|
||||||
|
const args = [_][]const u8{
|
||||||
|
"iptables", action, "OUTPUT", "-p", "tcp",
|
||||||
|
"-s", self.ip_str, "--sport", self.range_str, "--tcp-flags",
|
||||||
|
"RST", "RST", "-j", "DROP",
|
||||||
|
};
|
||||||
|
const res = std.process.run(self.allocator, self.io, .{ .argv = &args }) catch return error.IptablesSpawnFailed;
|
||||||
|
defer self.allocator.free(res.stdout);
|
||||||
|
defer self.allocator.free(res.stderr);
|
||||||
|
switch (res.term) {
|
||||||
|
.exited => |code| if (code != 0) return error.IptablesFailed,
|
||||||
|
else => return error.IptablesFailed,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cleanupHint(self: *const RstSuppressor, buf: []u8) []const u8 {
|
||||||
|
return std.fmt.bufPrint(buf, "iptables -D OUTPUT -p tcp -s {s} --sport {s} --tcp-flags RST RST -j DROP", .{ self.ip_str, self.range_str }) catch "";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn teardown(self: *RstSuppressor) void {
|
||||||
|
if (self.installed) {
|
||||||
|
self.runIptables("-D") catch {};
|
||||||
|
self.installed = false;
|
||||||
|
}
|
||||||
|
self.allocator.free(self.ip_str);
|
||||||
|
self.allocator.free(self.range_str);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const omitted_help =
|
||||||
|
\\ deliberately omitted (obsolete in 2026; rationale + citations)
|
||||||
|
\\ idle/zombie scan modern OSes randomize IP-ID; the side channel is dead
|
||||||
|
\\ fragmentation Snort 3.x and Suricata fully reassemble before matching
|
||||||
|
\\ TTL manipulation inline IPS normalize TTL; FortiGuard ships a signature
|
||||||
|
\\ MAC / source routing L2-only or RFC 5095-deprecated; never crosses a hop
|
||||||
|
\\ bad-checksum probe a firewall-reveal recon trick, not evasion
|
||||||
|
;
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn testIo() std.Io.Threaded {
|
||||||
|
return std.Io.Threaded.init(std.testing.allocator, .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
test "no stealth flags yields the inert default config" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
const args = [_][]const u8{ "scan", "--target", "10.0.0.0/24" };
|
||||||
|
var cfg = try parse(std.testing.allocator, threaded.io(), &args);
|
||||||
|
defer cfg.deinit(std.testing.allocator);
|
||||||
|
try std.testing.expect(!cfg.authorized);
|
||||||
|
try std.testing.expectEqual(packet.OsProfile.none, cfg.profile);
|
||||||
|
try std.testing.expectEqual(packet.ScanType.syn, cfg.scan);
|
||||||
|
try std.testing.expect(!cfg.jitter and !cfg.rotate and !cfg.suppress_rst);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "any stealth flag without --authorized-scan is refused" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
inline for (.{
|
||||||
|
&[_][]const u8{ "scan", "--os-template", "linux" },
|
||||||
|
&[_][]const u8{ "scan", "--scan-type", "fin" },
|
||||||
|
&[_][]const u8{ "scan", "--jitter", "poisson" },
|
||||||
|
&[_][]const u8{ "scan", "--source-port-rotation" },
|
||||||
|
&[_][]const u8{ "scan", "--decoys", "8.8.8.8" },
|
||||||
|
&[_][]const u8{ "scan", "--suppress-rst" },
|
||||||
|
}) |args| {
|
||||||
|
try std.testing.expectError(error.AuthorizationRequired, parse(std.testing.allocator, threaded.io(), args));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "authorized stealth parses every knob" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
const args = [_][]const u8{
|
||||||
|
"scan", "--authorized-scan", "--os-template", "windows",
|
||||||
|
"--scan-type", "ack", "--jitter", "poisson",
|
||||||
|
"--source-port-rotation", "--suppress-rst",
|
||||||
|
};
|
||||||
|
var cfg = try parse(std.testing.allocator, threaded.io(), &args);
|
||||||
|
defer cfg.deinit(std.testing.allocator);
|
||||||
|
try std.testing.expect(cfg.authorized);
|
||||||
|
try std.testing.expectEqual(packet.OsProfile.windows, cfg.profile);
|
||||||
|
try std.testing.expectEqual(packet.ScanType.ack, cfg.scan);
|
||||||
|
try std.testing.expect(cfg.jitter and cfg.rotate and cfg.suppress_rst);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "bad stealth values are rejected with distinct errors" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
const io = threaded.io();
|
||||||
|
try std.testing.expectError(error.BadOsTemplate, parse(std.testing.allocator, io, &[_][]const u8{ "scan", "--authorized-scan", "--os-template", "plan9" }));
|
||||||
|
try std.testing.expectError(error.BadScanType, parse(std.testing.allocator, io, &[_][]const u8{ "scan", "--authorized-scan", "--scan-type", "banana" }));
|
||||||
|
try std.testing.expectError(error.BadJitterMode, parse(std.testing.allocator, io, &[_][]const u8{ "scan", "--authorized-scan", "--jitter", "chaos" }));
|
||||||
|
try std.testing.expectError(error.BadDecoySpec, parse(std.testing.allocator, io, &[_][]const u8{ "scan", "--authorized-scan", "--decoys", "999.1.1.1" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "explicit decoys parse to their addresses" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
const args = [_][]const u8{ "scan", "--authorized-scan", "--decoys", "8.8.8.8,1.1.1.1" };
|
||||||
|
var cfg = try parse(std.testing.allocator, threaded.io(), &args);
|
||||||
|
defer cfg.deinit(std.testing.allocator);
|
||||||
|
try std.testing.expectEqualSlices(u32, &.{ 0x08080808, 0x01010101 }, cfg.decoys);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "RND decoys are non-bogon and bounded" {
|
||||||
|
var threaded = testIo();
|
||||||
|
defer threaded.deinit();
|
||||||
|
const args = [_][]const u8{ "scan", "--authorized-scan", "--decoys", "RND:8" };
|
||||||
|
var cfg = try parse(std.testing.allocator, threaded.io(), &args);
|
||||||
|
defer cfg.deinit(std.testing.allocator);
|
||||||
|
try std.testing.expectEqual(@as(usize, 8), cfg.decoys.len);
|
||||||
|
for (cfg.decoys) |ip| try std.testing.expect(!isBogonV4(ip));
|
||||||
|
|
||||||
|
try std.testing.expectError(error.TooManyDecoys, parse(std.testing.allocator, threaded.io(), &[_][]const u8{ "scan", "--authorized-scan", "--decoys", "RND:99" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "isBogonV4 flags reserved space and passes public addresses" {
|
||||||
|
try std.testing.expect(isBogonV4(0x00000001));
|
||||||
|
try std.testing.expect(isBogonV4(0x0A000001));
|
||||||
|
try std.testing.expect(isBogonV4(0x7F000001));
|
||||||
|
try std.testing.expect(isBogonV4(0xC0A80001));
|
||||||
|
try std.testing.expect(isBogonV4(0xAC100001));
|
||||||
|
try std.testing.expect(isBogonV4(0xA9FE0001));
|
||||||
|
try std.testing.expect(isBogonV4(0x64400001));
|
||||||
|
try std.testing.expect(isBogonV4(0xE0000001));
|
||||||
|
try std.testing.expect(isBogonV4(0xFFFFFFFF));
|
||||||
|
try std.testing.expect(isBogonV4(0xC0586301));
|
||||||
|
try std.testing.expect(!isBogonV4(0x08080808));
|
||||||
|
try std.testing.expect(!isBogonV4(0x01010101));
|
||||||
|
try std.testing.expect(!isBogonV4(0x2D2D2D2D));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ipToStr renders dotted quads" {
|
||||||
|
var buf: [15]u8 = undefined;
|
||||||
|
try std.testing.expectEqualStrings("10.0.0.1", ipToStr(&buf, 0x0A000001));
|
||||||
|
try std.testing.expectEqualStrings("255.255.255.255", ipToStr(&buf, 0xFFFFFFFF));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the RST cleanup hint is an exact iptables delete line" {
|
||||||
|
var buf: [15]u8 = undefined;
|
||||||
|
var rbuf: [16]u8 = undefined;
|
||||||
|
const ip_str = std.testing.allocator.dupe(u8, ipToStr(&buf, 0x0A000001)) catch unreachable;
|
||||||
|
defer std.testing.allocator.free(ip_str);
|
||||||
|
const range_str = std.fmt.bufPrint(&rbuf, "{d}:{d}", .{ 40000, 48191 }) catch unreachable;
|
||||||
|
const owned_range = std.testing.allocator.dupe(u8, range_str) catch unreachable;
|
||||||
|
defer std.testing.allocator.free(owned_range);
|
||||||
|
var supp = RstSuppressor{ .allocator = std.testing.allocator, .io = undefined, .ip_str = ip_str, .range_str = owned_range, .installed = false };
|
||||||
|
var hintbuf: [128]u8 = undefined;
|
||||||
|
const hint = supp.cleanupHint(&hintbuf);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, hint, "iptables -D OUTPUT") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, hint, "10.0.0.1") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, hint, "40000:48191") != null);
|
||||||
|
try std.testing.expect(std.mem.indexOf(u8, hint, "--tcp-flags RST RST -j DROP") != null);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,526 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// targets.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const numtheory = @import("numtheory");
|
||||||
|
const netutil = @import("netutil");
|
||||||
|
|
||||||
|
pub const Range = struct {
|
||||||
|
start: u32,
|
||||||
|
end: u32,
|
||||||
|
|
||||||
|
pub fn count(self: Range) u64 {
|
||||||
|
return @as(u64, self.end - self.start) + 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const reserved = [_]Range{
|
||||||
|
.{ .start = 0x00000000, .end = 0x00ffffff },
|
||||||
|
.{ .start = 0x0a000000, .end = 0x0affffff },
|
||||||
|
.{ .start = 0x64400000, .end = 0x647fffff },
|
||||||
|
.{ .start = 0x7f000000, .end = 0x7fffffff },
|
||||||
|
.{ .start = 0xa9fe0000, .end = 0xa9feffff },
|
||||||
|
.{ .start = 0xac100000, .end = 0xac1fffff },
|
||||||
|
.{ .start = 0xc0000000, .end = 0xc00000ff },
|
||||||
|
.{ .start = 0xc0000200, .end = 0xc00002ff },
|
||||||
|
.{ .start = 0xc0586300, .end = 0xc05863ff },
|
||||||
|
.{ .start = 0xc0a80000, .end = 0xc0a8ffff },
|
||||||
|
.{ .start = 0xc6120000, .end = 0xc613ffff },
|
||||||
|
.{ .start = 0xc6336400, .end = 0xc63364ff },
|
||||||
|
.{ .start = 0xcb007100, .end = 0xcb0071ff },
|
||||||
|
.{ .start = 0xe0000000, .end = 0xefffffff },
|
||||||
|
.{ .start = 0xf0000000, .end = 0xffffffff },
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn parseCidr(text: []const u8) !Range {
|
||||||
|
const slash = std.mem.indexOfScalar(u8, text, '/') orelse return error.InvalidCidr;
|
||||||
|
const addr_text = text[0..slash];
|
||||||
|
const prefix = std.fmt.parseInt(u6, text[slash + 1 ..], 10) catch return error.InvalidCidr;
|
||||||
|
if (prefix > 32) return error.InvalidCidr;
|
||||||
|
|
||||||
|
var base: u32 = 0;
|
||||||
|
var octets: usize = 0;
|
||||||
|
var it = std.mem.splitScalar(u8, addr_text, '.');
|
||||||
|
while (it.next()) |part| {
|
||||||
|
if (octets == 4) return error.InvalidCidr;
|
||||||
|
const octet = std.fmt.parseInt(u8, part, 10) catch return error.InvalidCidr;
|
||||||
|
base = (base << 8) | octet;
|
||||||
|
octets += 1;
|
||||||
|
}
|
||||||
|
if (octets != 4) return error.InvalidCidr;
|
||||||
|
|
||||||
|
const host_bits: u6 = @intCast(32 - @as(u32, prefix));
|
||||||
|
if (host_bits == 32) return .{ .start = 0, .end = 0xffffffff };
|
||||||
|
const sh: u5 = @intCast(host_bits);
|
||||||
|
const span: u32 = (@as(u32, 1) << sh) - 1;
|
||||||
|
const start = base & ~span;
|
||||||
|
return .{ .start = start, .end = start | span };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isReserved(ip: u32) bool {
|
||||||
|
var lo: usize = 0;
|
||||||
|
var hi: usize = reserved.len;
|
||||||
|
while (lo < hi) {
|
||||||
|
const mid = lo + (hi - lo) / 2;
|
||||||
|
if (ip < reserved[mid].start) {
|
||||||
|
hi = mid;
|
||||||
|
} else if (ip > reserved[mid].end) {
|
||||||
|
lo = mid + 1;
|
||||||
|
} else return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn subtractReserved(allocator: std.mem.Allocator, acc: *std.ArrayList(Range), r: Range) !void {
|
||||||
|
var pending: std.ArrayList(Range) = .empty;
|
||||||
|
defer pending.deinit(allocator);
|
||||||
|
try pending.append(allocator, r);
|
||||||
|
for (reserved) |res| {
|
||||||
|
var next: std.ArrayList(Range) = .empty;
|
||||||
|
errdefer next.deinit(allocator);
|
||||||
|
for (pending.items) |cur| {
|
||||||
|
if (res.end < cur.start or res.start > cur.end) {
|
||||||
|
try next.append(allocator, cur);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cur.start < res.start) try next.append(allocator, .{ .start = cur.start, .end = res.start - 1 });
|
||||||
|
if (cur.end > res.end) try next.append(allocator, .{ .start = res.end + 1, .end = cur.end });
|
||||||
|
}
|
||||||
|
pending.deinit(allocator);
|
||||||
|
pending = next;
|
||||||
|
}
|
||||||
|
for (pending.items) |s| try acc.append(allocator, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const IpPicker = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
ranges: []Range,
|
||||||
|
prefix: []u64,
|
||||||
|
count: u64,
|
||||||
|
|
||||||
|
pub fn build(allocator: std.mem.Allocator, user: []const Range) !IpPicker {
|
||||||
|
var acc: std.ArrayList(Range) = .empty;
|
||||||
|
defer acc.deinit(allocator);
|
||||||
|
for (user) |r| try subtractReserved(allocator, &acc, r);
|
||||||
|
std.mem.sort(Range, acc.items, {}, struct {
|
||||||
|
fn lt(_: void, a: Range, b: Range) bool {
|
||||||
|
return a.start < b.start;
|
||||||
|
}
|
||||||
|
}.lt);
|
||||||
|
|
||||||
|
const ranges = try allocator.dupe(Range, acc.items);
|
||||||
|
errdefer allocator.free(ranges);
|
||||||
|
const prefix = try allocator.alloc(u64, ranges.len + 1);
|
||||||
|
var total: u64 = 0;
|
||||||
|
for (ranges, 0..) |r, k| {
|
||||||
|
prefix[k] = total;
|
||||||
|
total += r.count();
|
||||||
|
}
|
||||||
|
prefix[ranges.len] = total;
|
||||||
|
return .{ .allocator = allocator, .ranges = ranges, .prefix = prefix, .count = total };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *IpPicker) void {
|
||||||
|
self.allocator.free(self.ranges);
|
||||||
|
self.allocator.free(self.prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn at(self: IpPicker, index: u64) u32 {
|
||||||
|
std.debug.assert(index < self.count);
|
||||||
|
var lo: usize = 0;
|
||||||
|
var hi: usize = self.ranges.len;
|
||||||
|
while (lo + 1 < hi) {
|
||||||
|
const mid = lo + (hi - lo) / 2;
|
||||||
|
if (self.prefix[mid] <= index) lo = mid else hi = mid;
|
||||||
|
}
|
||||||
|
const offset: u32 = @intCast(index - self.prefix[lo]);
|
||||||
|
return self.ranges[lo].start + offset;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Target = struct {
|
||||||
|
ip: u32,
|
||||||
|
port: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Engine = struct {
|
||||||
|
picker: IpPicker,
|
||||||
|
ports: []u16,
|
||||||
|
num_ports: u64,
|
||||||
|
total: u64,
|
||||||
|
prime: u64,
|
||||||
|
generator: u64,
|
||||||
|
current: u64,
|
||||||
|
steps_left: u64,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator, cidrs: []const Range, ports: []const u16, seed: u64) !Engine {
|
||||||
|
return initShard(allocator, cidrs, ports, seed, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initShard(
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
cidrs: []const Range,
|
||||||
|
ports: []const u16,
|
||||||
|
seed: u64,
|
||||||
|
num_shards: u64,
|
||||||
|
shard_id: u64,
|
||||||
|
) !Engine {
|
||||||
|
var picker = try IpPicker.build(allocator, cidrs);
|
||||||
|
errdefer picker.deinit();
|
||||||
|
const ports_copy = try allocator.dupe(u16, ports);
|
||||||
|
errdefer allocator.free(ports_copy);
|
||||||
|
|
||||||
|
const num_ports: u64 = @intCast(ports.len);
|
||||||
|
const total = picker.count * num_ports;
|
||||||
|
const prime = numtheory.smallestPrimeAbove(total);
|
||||||
|
const order = prime - 1;
|
||||||
|
if (num_shards == 0 or shard_id >= num_shards or num_shards > order) return error.InvalidShardCount;
|
||||||
|
|
||||||
|
var prng = std.Random.DefaultPrng.init(seed);
|
||||||
|
const rand = prng.random();
|
||||||
|
const generator = numtheory.findPrimitiveRoot(prime, rand);
|
||||||
|
const start = rand.intRangeAtMost(u64, 1, prime - 1);
|
||||||
|
|
||||||
|
const chunk = order / num_shards;
|
||||||
|
const begin = shard_id * chunk;
|
||||||
|
const my_steps = if (shard_id == num_shards - 1) order - begin else chunk;
|
||||||
|
const offset = numtheory.modExp(generator, begin, prime);
|
||||||
|
const current = numtheory.mulMod(start, offset, prime);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.picker = picker,
|
||||||
|
.ports = ports_copy,
|
||||||
|
.num_ports = num_ports,
|
||||||
|
.total = total,
|
||||||
|
.prime = prime,
|
||||||
|
.generator = generator,
|
||||||
|
.current = current,
|
||||||
|
.steps_left = my_steps,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Engine) void {
|
||||||
|
const allocator = self.picker.allocator;
|
||||||
|
self.picker.deinit();
|
||||||
|
allocator.free(self.ports);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(self: *Engine) ?Target {
|
||||||
|
while (self.steps_left > 0) {
|
||||||
|
self.current = numtheory.mulMod(self.current, self.generator, self.prime);
|
||||||
|
self.steps_left -= 1;
|
||||||
|
const idx = self.current;
|
||||||
|
if (idx >= 1 and idx <= self.total) {
|
||||||
|
const idx0 = idx - 1;
|
||||||
|
const ip_pos = idx0 / self.num_ports;
|
||||||
|
const port_pos = idx0 % self.num_ports;
|
||||||
|
return .{ .ip = self.picker.at(ip_pos), .port = self.ports[@intCast(port_pos)] };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const default_max_hosts6: u64 = 1 << 20;
|
||||||
|
|
||||||
|
pub const Cidr6 = struct {
|
||||||
|
base: [16]u8,
|
||||||
|
prefix: u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
const Reserved6 = struct { prefix: [16]u8, bits: u8 };
|
||||||
|
|
||||||
|
const reserved6 = [_]Reserved6{
|
||||||
|
.{ .prefix = [_]u8{0} ** 16, .bits = 128 },
|
||||||
|
.{ .prefix = [_]u8{0} ** 15 ++ [_]u8{1}, .bits = 128 },
|
||||||
|
.{ .prefix = [_]u8{0} ** 10 ++ [_]u8{ 0xff, 0xff } ++ [_]u8{0} ** 4, .bits = 96 },
|
||||||
|
.{ .prefix = [_]u8{ 0x01, 0x00 } ++ [_]u8{0} ** 14, .bits = 8 },
|
||||||
|
.{ .prefix = [_]u8{ 0x20, 0x01, 0x0d, 0xb8 } ++ [_]u8{0} ** 12, .bits = 32 },
|
||||||
|
.{ .prefix = [_]u8{ 0xfc, 0x00 } ++ [_]u8{0} ** 14, .bits = 7 },
|
||||||
|
.{ .prefix = [_]u8{ 0xfe, 0x80 } ++ [_]u8{0} ** 14, .bits = 10 },
|
||||||
|
.{ .prefix = [_]u8{ 0xff, 0x00 } ++ [_]u8{0} ** 14, .bits = 8 },
|
||||||
|
};
|
||||||
|
|
||||||
|
fn inPrefix6(addr: [16]u8, prefix: [16]u8, bits: u8) bool {
|
||||||
|
const full = bits / 8;
|
||||||
|
for (0..full) |i| if (addr[i] != prefix[i]) return false;
|
||||||
|
const rem: u3 = @intCast(bits % 8);
|
||||||
|
if (rem != 0) {
|
||||||
|
const mask: u8 = @as(u8, 0xff) << @intCast(8 - @as(u4, rem));
|
||||||
|
if ((addr[full] & mask) != (prefix[full] & mask)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn isReserved6(addr: [16]u8) bool {
|
||||||
|
for (reserved6) |res| {
|
||||||
|
if (inPrefix6(addr, res.prefix, res.bits)) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn maskAddr6(addr: [16]u8, prefix: u8) [16]u8 {
|
||||||
|
var out = addr;
|
||||||
|
var bit: usize = prefix;
|
||||||
|
while (bit < 128) : (bit += 1) {
|
||||||
|
const byte = bit / 8;
|
||||||
|
const off: u3 = @intCast(7 - (bit % 8));
|
||||||
|
out[byte] &= ~(@as(u8, 1) << off);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parseCidr6(text: []const u8) !Cidr6 {
|
||||||
|
const slash = std.mem.indexOfScalar(u8, text, '/') orelse return error.InvalidCidr;
|
||||||
|
const addr = try netutil.parseIpv6(text[0..slash]);
|
||||||
|
const prefix = std.fmt.parseInt(u8, text[slash + 1 ..], 10) catch return error.InvalidCidr;
|
||||||
|
if (prefix > 128) return error.InvalidCidr;
|
||||||
|
if (prefix == 0) return error.PrefixTooLarge;
|
||||||
|
return .{ .base = maskAddr6(addr, prefix), .prefix = prefix };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Target6 = struct {
|
||||||
|
addr: [16]u8,
|
||||||
|
port: u16,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Engine6 = struct {
|
||||||
|
base: [16]u8,
|
||||||
|
ports: []u16,
|
||||||
|
num_ports: u64,
|
||||||
|
host_count: u64,
|
||||||
|
total: u64,
|
||||||
|
prime: u64,
|
||||||
|
generator: u64,
|
||||||
|
current: u64,
|
||||||
|
steps_left: u64,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator, cidr: Cidr6, ports: []const u16, seed: u64, max_hosts: u64) !Engine6 {
|
||||||
|
const host_bits: u8 = 128 - cidr.prefix;
|
||||||
|
if (host_bits >= 64) return error.PrefixTooLarge;
|
||||||
|
const host_count: u64 = if (host_bits == 0) 1 else (@as(u64, 1) << @intCast(host_bits));
|
||||||
|
if (host_count > max_hosts) return error.PrefixTooLarge;
|
||||||
|
|
||||||
|
const ports_copy = try allocator.dupe(u16, ports);
|
||||||
|
errdefer allocator.free(ports_copy);
|
||||||
|
const num_ports: u64 = @intCast(ports.len);
|
||||||
|
if (num_ports == 0 or host_count > std.math.maxInt(u64) / num_ports) return error.PrefixTooLarge;
|
||||||
|
const total = host_count * num_ports;
|
||||||
|
const prime = numtheory.smallestPrimeAbove(total);
|
||||||
|
|
||||||
|
var prng = std.Random.DefaultPrng.init(seed);
|
||||||
|
const rand = prng.random();
|
||||||
|
const generator = numtheory.findPrimitiveRoot(prime, rand);
|
||||||
|
const start = rand.intRangeAtMost(u64, 1, prime - 1);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.base = cidr.base,
|
||||||
|
.ports = ports_copy,
|
||||||
|
.num_ports = num_ports,
|
||||||
|
.host_count = host_count,
|
||||||
|
.total = total,
|
||||||
|
.prime = prime,
|
||||||
|
.generator = generator,
|
||||||
|
.current = start,
|
||||||
|
.steps_left = prime - 1,
|
||||||
|
.allocator = allocator,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Engine6) void {
|
||||||
|
self.allocator.free(self.ports);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn addrAt(self: *const Engine6, host_index: u64) [16]u8 {
|
||||||
|
var addr = self.base;
|
||||||
|
const lo = std.mem.readInt(u64, addr[8..16], .big);
|
||||||
|
std.mem.writeInt(u64, addr[8..16], lo | host_index, .big);
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next(self: *Engine6) ?Target6 {
|
||||||
|
while (self.steps_left > 0) {
|
||||||
|
self.current = numtheory.mulMod(self.current, self.generator, self.prime);
|
||||||
|
self.steps_left -= 1;
|
||||||
|
const idx = self.current;
|
||||||
|
if (idx >= 1 and idx <= self.total) {
|
||||||
|
const idx0 = idx - 1;
|
||||||
|
const host_pos = idx0 / self.num_ports;
|
||||||
|
const port_pos = idx0 % self.num_ports;
|
||||||
|
const addr = self.addrAt(host_pos);
|
||||||
|
if (isReserved6(addr)) continue;
|
||||||
|
return .{ .addr = addr, .port = self.ports[@intCast(port_pos)] };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "parseCidr yields the right range and count" {
|
||||||
|
const a = try parseCidr("10.0.0.0/24");
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x0a000000), a.start);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x0a0000ff), a.end);
|
||||||
|
try std.testing.expectEqual(@as(u64, 256), a.count());
|
||||||
|
|
||||||
|
const b = try parseCidr("192.168.1.0/30");
|
||||||
|
try std.testing.expectEqual(@as(u64, 4), b.count());
|
||||||
|
|
||||||
|
const h = try parseCidr("8.8.8.8/32");
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x08080808), h.start);
|
||||||
|
try std.testing.expectEqual(@as(u64, 1), h.count());
|
||||||
|
|
||||||
|
try std.testing.expectError(error.InvalidCidr, parseCidr("999.0.0.0/8"));
|
||||||
|
try std.testing.expectError(error.InvalidCidr, parseCidr("10.0.0.0/33"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "isReserved flags RFC 6890 space, passes public IPs" {
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("127.0.0.1/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("10.1.2.3/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("192.168.1.1/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("169.254.5.5/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("224.0.0.1/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("0.0.0.0/32")).start));
|
||||||
|
try std.testing.expect(isReserved((try parseCidr("192.88.99.1/32")).start));
|
||||||
|
try std.testing.expect(!isReserved((try parseCidr("8.8.8.8/32")).start));
|
||||||
|
try std.testing.expect(!isReserved((try parseCidr("1.1.1.1/32")).start));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "IpPicker maps indices across user CIDRs minus the reserved floor" {
|
||||||
|
const cidrs = [_]Range{
|
||||||
|
try parseCidr("8.8.8.0/30"),
|
||||||
|
try parseCidr("10.0.0.0/24"),
|
||||||
|
try parseCidr("1.1.1.0/31"),
|
||||||
|
};
|
||||||
|
var picker = try IpPicker.build(std.testing.allocator, &cidrs);
|
||||||
|
defer picker.deinit();
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u64, 6), picker.count);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x01010100), picker.at(0));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x01010101), picker.at(1));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x08080800), picker.at(2));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x08080803), picker.at(5));
|
||||||
|
var i: u64 = 0;
|
||||||
|
while (i < picker.count) : (i += 1) try std.testing.expect(!isReserved(picker.at(i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "IpPicker over a fully reserved input is empty" {
|
||||||
|
const cidrs = [_]Range{try parseCidr("192.168.0.0/16")};
|
||||||
|
var picker = try IpPicker.build(std.testing.allocator, &cidrs);
|
||||||
|
defer picker.deinit();
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), picker.count);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Engine is a bijection: every IP:port hit exactly once" {
|
||||||
|
const cidrs = [_]Range{ try parseCidr("8.8.8.0/28"), try parseCidr("1.2.3.0/30") };
|
||||||
|
const ports = [_]u16{ 80, 443, 22 };
|
||||||
|
var eng = try Engine.init(std.testing.allocator, &cidrs, &ports, 0xDEADBEEF);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u64, 60), eng.total);
|
||||||
|
var seen = std.AutoHashMap(u64, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
var n: u64 = 0;
|
||||||
|
while (eng.next()) |t| {
|
||||||
|
try std.testing.expect(!isReserved(t.ip));
|
||||||
|
const key = (@as(u64, t.ip) << 16) | t.port;
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(u64, 60), n);
|
||||||
|
try std.testing.expectEqual(@as(u64, 60), seen.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "shards with a shared seed union to the full bijection with no overlap" {
|
||||||
|
const cidrs = [_]Range{try parseCidr("8.8.8.0/27")};
|
||||||
|
const ports = [_]u16{ 80, 443 };
|
||||||
|
const seed: u64 = 0x1234_5678;
|
||||||
|
const num_shards: u64 = 4;
|
||||||
|
|
||||||
|
var seen = std.AutoHashMap(u64, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
var emitted: u64 = 0;
|
||||||
|
var s: u64 = 0;
|
||||||
|
while (s < num_shards) : (s += 1) {
|
||||||
|
var eng = try Engine.initShard(std.testing.allocator, &cidrs, &ports, seed, num_shards, s);
|
||||||
|
defer eng.deinit();
|
||||||
|
while (eng.next()) |t| {
|
||||||
|
const key = (@as(u64, t.ip) << 16) | t.port;
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
emitted += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(u64, 64), emitted);
|
||||||
|
try std.testing.expectEqual(@as(u64, 64), seen.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "initShard rejects nonsensical shard counts" {
|
||||||
|
const cidrs = [_]Range{try parseCidr("8.8.8.0/30")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
try std.testing.expectError(error.InvalidShardCount, Engine.initShard(std.testing.allocator, &cidrs, &ports, 1, 0, 0));
|
||||||
|
try std.testing.expectError(error.InvalidShardCount, Engine.initShard(std.testing.allocator, &cidrs, &ports, 1, 100, 0));
|
||||||
|
try std.testing.expectError(error.InvalidShardCount, Engine.initShard(std.testing.allocator, &cidrs, &ports, 1, 2, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "parseCidr6 masks host bits, rejects ::/0 and bad prefixes" {
|
||||||
|
const c = try parseCidr6("2001:db8:1:2:3:4:5:6/64");
|
||||||
|
try std.testing.expectEqual([16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0 }, c.base);
|
||||||
|
try std.testing.expectEqual(@as(u8, 64), c.prefix);
|
||||||
|
|
||||||
|
const c120 = try parseCidr6("2001:470:1:2::ab/120");
|
||||||
|
try std.testing.expectEqual(@as(u8, 0), c120.base[15]);
|
||||||
|
|
||||||
|
try std.testing.expectError(error.PrefixTooLarge, parseCidr6("::/0"));
|
||||||
|
try std.testing.expectError(error.InvalidCidr, parseCidr6("2001:db8::/129"));
|
||||||
|
try std.testing.expectError(error.InvalidCidr, parseCidr6("2001:db8::"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "isReserved6 flags special-use IPv6 blocks and passes global space" {
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("::1")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("::")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("fe80::1")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("fc00::1")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("ff02::1")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("2001:db8::1")));
|
||||||
|
try std.testing.expect(isReserved6(try netutil.parseIpv6("::ffff:c0a8:1")));
|
||||||
|
try std.testing.expect(!isReserved6(try netutil.parseIpv6("2001:470:1:2::5")));
|
||||||
|
try std.testing.expect(!isReserved6(try netutil.parseIpv6("2606:4700:4700::1111")));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Engine6 is a bijection over a bounded prefix, every addr:port once, none reserved" {
|
||||||
|
const cidr = try parseCidr6("2001:470:1:2::/120");
|
||||||
|
const ports = [_]u16{ 80, 443 };
|
||||||
|
var eng = try Engine6.init(std.testing.allocator, cidr, &ports, 0xC0FFEE, default_max_hosts6);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u64, 512), eng.total);
|
||||||
|
var seen = std.AutoHashMap(u160Key, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
var n: u64 = 0;
|
||||||
|
while (eng.next()) |t| {
|
||||||
|
try std.testing.expect(!isReserved6(t.addr));
|
||||||
|
const key = u160Key{ .addr = t.addr, .port = t.port };
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
n += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(u64, 512), n);
|
||||||
|
}
|
||||||
|
|
||||||
|
const u160Key = struct { addr: [16]u8, port: u16 };
|
||||||
|
|
||||||
|
test "Engine6 rejects prefixes whose host space is too large" {
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
try std.testing.expectError(error.PrefixTooLarge, Engine6.init(std.testing.allocator, try parseCidr6("2001:470::/64"), &ports, 1, default_max_hosts6));
|
||||||
|
try std.testing.expectError(error.PrefixTooLarge, Engine6.init(std.testing.allocator, try parseCidr6("2001:470::/100"), &ports, 1, default_max_hosts6));
|
||||||
|
var eng = try Engine6.init(std.testing.allocator, try parseCidr6("2001:470::/112"), &ports, 1, default_max_hosts6);
|
||||||
|
eng.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Engine6 rejects a host-times-port product that would overflow u64" {
|
||||||
|
const ports = [_]u16{ 1, 2, 3, 4 };
|
||||||
|
const cidr = try parseCidr6("2001:470::/65");
|
||||||
|
try std.testing.expectError(error.PrefixTooLarge, Engine6.init(std.testing.allocator, cidr, &ports, 1, std.math.maxInt(u64)));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,555 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// template.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const packet = @import("packet");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
|
||||||
|
const ethertype_ipv4: u16 = 0x0800;
|
||||||
|
const ipv4_version_ihl: u8 = 0x45;
|
||||||
|
const ip_proto_tcp: u8 = 6;
|
||||||
|
const ip_flag_dont_fragment: u16 = 0x4000;
|
||||||
|
const default_ttl: u8 = 64;
|
||||||
|
const ip_id_mix: u32 = 0x9e3779b1;
|
||||||
|
|
||||||
|
const eth_len: usize = 14;
|
||||||
|
const ip_len: usize = 20;
|
||||||
|
const tcp_len: usize = 20;
|
||||||
|
const l4_off: usize = eth_len + ip_len;
|
||||||
|
const opts_off: usize = l4_off + tcp_len;
|
||||||
|
|
||||||
|
const ip_id_off: usize = eth_len + 4;
|
||||||
|
const ip_checksum_off: usize = eth_len + 10;
|
||||||
|
const ip_src_off: usize = eth_len + 12;
|
||||||
|
const ip_dst_off: usize = eth_len + 16;
|
||||||
|
const tcp_src_off: usize = l4_off;
|
||||||
|
const tcp_dst_off: usize = l4_off + 2;
|
||||||
|
const tcp_seq_off: usize = l4_off + 4;
|
||||||
|
const tcp_ack_off: usize = l4_off + 8;
|
||||||
|
const tcp_data_off_off: usize = l4_off + 12;
|
||||||
|
const tcp_flags_off: usize = l4_off + 13;
|
||||||
|
const tcp_window_off: usize = l4_off + 14;
|
||||||
|
const tcp_checksum_off: usize = l4_off + 16;
|
||||||
|
|
||||||
|
pub const SynTemplate = struct {
|
||||||
|
pub const max_frame_len: usize = opts_off + packet.max_syn_options_len;
|
||||||
|
|
||||||
|
base: [max_frame_len]u8,
|
||||||
|
frame_len: usize,
|
||||||
|
src_ip: u32,
|
||||||
|
src_ip_be: u32,
|
||||||
|
src_port: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
scan: packet.ScanType,
|
||||||
|
vary_ip_id: bool,
|
||||||
|
rotate: bool,
|
||||||
|
rotate_base: u16,
|
||||||
|
rotate_span: u16,
|
||||||
|
decoys: []const u32,
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
src_mac: [6]u8,
|
||||||
|
dst_mac: [6]u8,
|
||||||
|
src_ip: u32,
|
||||||
|
src_port: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
profile: packet.OsProfile = .none,
|
||||||
|
scan: packet.ScanType = .syn,
|
||||||
|
rotate: bool = false,
|
||||||
|
rotate_base: u16 = 0,
|
||||||
|
rotate_span: u16 = 0,
|
||||||
|
decoys: []const u32 = &.{},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init(cfg: Config) SynTemplate {
|
||||||
|
const opts = cfg.profile.options();
|
||||||
|
const tcp_total = tcp_len + opts.len;
|
||||||
|
const data_off_words: u8 = @intCast(tcp_total / 4);
|
||||||
|
|
||||||
|
var base: [max_frame_len]u8 = [_]u8{0} ** max_frame_len;
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = cfg.dst_mac,
|
||||||
|
.src = cfg.src_mac,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, ethertype_ipv4),
|
||||||
|
};
|
||||||
|
@memcpy(base[0..eth_len], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
const ip = packet.Ipv4Hdr{
|
||||||
|
.version_ihl = ipv4_version_ihl,
|
||||||
|
.tos = 0,
|
||||||
|
.total_len = std.mem.nativeToBig(u16, @intCast(ip_len + tcp_total)),
|
||||||
|
.id = 0,
|
||||||
|
.flags_frag = std.mem.nativeToBig(u16, ip_flag_dont_fragment),
|
||||||
|
.ttl = default_ttl,
|
||||||
|
.protocol = ip_proto_tcp,
|
||||||
|
.checksum = 0,
|
||||||
|
.src = std.mem.nativeToBig(u32, cfg.src_ip),
|
||||||
|
.dst = 0,
|
||||||
|
};
|
||||||
|
@memcpy(base[eth_len..l4_off], std.mem.asBytes(&ip));
|
||||||
|
|
||||||
|
const tcp = packet.TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, cfg.src_port),
|
||||||
|
.dst_port = 0,
|
||||||
|
.seq = 0,
|
||||||
|
.ack = 0,
|
||||||
|
.data_off_ns = data_off_words << 4,
|
||||||
|
.flags = cfg.scan.probeFlags(),
|
||||||
|
.window = std.mem.nativeToBig(u16, cfg.profile.window()),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
@memcpy(base[l4_off..opts_off], std.mem.asBytes(&tcp));
|
||||||
|
|
||||||
|
if (opts.len > 0) @memcpy(base[opts_off .. opts_off + opts.len], opts);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.base = base,
|
||||||
|
.frame_len = opts_off + opts.len,
|
||||||
|
.src_ip = cfg.src_ip,
|
||||||
|
.src_ip_be = std.mem.nativeToBig(u32, cfg.src_ip),
|
||||||
|
.src_port = cfg.src_port,
|
||||||
|
.cookie = cfg.cookie,
|
||||||
|
.scan = cfg.scan,
|
||||||
|
.vary_ip_id = cfg.profile.variesIpId(),
|
||||||
|
.rotate = cfg.rotate,
|
||||||
|
.rotate_base = cfg.rotate_base,
|
||||||
|
.rotate_span = cfg.rotate_span,
|
||||||
|
.decoys = cfg.decoys,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variantCount(self: *const SynTemplate) usize {
|
||||||
|
return 1 + self.decoys.len;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn srcPortFor(self: *const SynTemplate, dst_ip: u32, dst_port: u16) u16 {
|
||||||
|
return if (self.rotate)
|
||||||
|
self.cookie.udpSrcPort(dst_ip, dst_port, self.src_ip, self.rotate_base, self.rotate_span)
|
||||||
|
else
|
||||||
|
self.src_port;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stamp(self: *const SynTemplate, out: *[max_frame_len]u8, dst_ip: u32, dst_port: u16) usize {
|
||||||
|
const n = self.frame_len;
|
||||||
|
@memcpy(out[0..n], self.base[0..n]);
|
||||||
|
|
||||||
|
if (self.vary_ip_id) {
|
||||||
|
const id: u16 = @truncate((dst_ip *% ip_id_mix) ^ @as(u32, dst_port));
|
||||||
|
std.mem.writeInt(u16, out[ip_id_off..][0..2], id, .big);
|
||||||
|
}
|
||||||
|
std.mem.writeInt(u32, out[ip_dst_off..][0..4], dst_ip, .big);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], 0, .big);
|
||||||
|
const ip_ck = packet.checksum(out[eth_len..l4_off]);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], ip_ck, .big);
|
||||||
|
|
||||||
|
const src_port = self.srcPortFor(dst_ip, dst_port);
|
||||||
|
std.mem.writeInt(u16, out[tcp_src_off..][0..2], src_port, .big);
|
||||||
|
std.mem.writeInt(u16, out[tcp_dst_off..][0..2], dst_port, .big);
|
||||||
|
|
||||||
|
const ck = self.cookie.seq(dst_ip, dst_port, self.src_ip, src_port);
|
||||||
|
if (self.scan.cookieInAck()) {
|
||||||
|
std.mem.writeInt(u32, out[tcp_seq_off..][0..4], 0, .big);
|
||||||
|
std.mem.writeInt(u32, out[tcp_ack_off..][0..4], ck, .big);
|
||||||
|
} else {
|
||||||
|
std.mem.writeInt(u32, out[tcp_seq_off..][0..4], ck, .big);
|
||||||
|
std.mem.writeInt(u32, out[tcp_ack_off..][0..4], 0, .big);
|
||||||
|
}
|
||||||
|
|
||||||
|
std.mem.writeInt(u16, out[tcp_checksum_off..][0..2], 0, .big);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, dst_ip);
|
||||||
|
const tcp_ck = packet.tcpChecksum(self.src_ip_be, dst_be, out[l4_off..n]);
|
||||||
|
std.mem.writeInt(u16, out[tcp_checksum_off..][0..2], tcp_ck, .big);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stampVariant(self: *const SynTemplate, out: *[max_frame_len]u8, dst_ip: u32, dst_port: u16, variant: usize) usize {
|
||||||
|
const n = self.stamp(out, dst_ip, dst_port);
|
||||||
|
if (variant == 0) return n;
|
||||||
|
|
||||||
|
const decoy_src = self.decoys[variant - 1];
|
||||||
|
const decoy_src_be = std.mem.nativeToBig(u32, decoy_src);
|
||||||
|
std.mem.writeInt(u32, out[ip_src_off..][0..4], decoy_src, .big);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], 0, .big);
|
||||||
|
const ip_ck = packet.checksum(out[eth_len..l4_off]);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], ip_ck, .big);
|
||||||
|
|
||||||
|
std.mem.writeInt(u16, out[tcp_checksum_off..][0..2], 0, .big);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, dst_ip);
|
||||||
|
const tcp_ck = packet.tcpChecksum(decoy_src_be, dst_be, out[l4_off..n]);
|
||||||
|
std.mem.writeInt(u16, out[tcp_checksum_off..][0..2], tcp_ck, .big);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const ethertype_ipv6: u16 = 0x86dd;
|
||||||
|
const ip6_version_tc_flow: u32 = 0x60000000;
|
||||||
|
const ip6_next_tcp: u8 = 6;
|
||||||
|
const default_hop_limit: u8 = 64;
|
||||||
|
|
||||||
|
const ip6_len: usize = 40;
|
||||||
|
const l4_off6: usize = eth_len + ip6_len;
|
||||||
|
const opts_off6: usize = l4_off6 + tcp_len;
|
||||||
|
|
||||||
|
const ip6_payload_len_off: usize = eth_len + 4;
|
||||||
|
const ip6_dst_off: usize = eth_len + 24;
|
||||||
|
const tcp6_src_off: usize = l4_off6;
|
||||||
|
const tcp6_dst_off: usize = l4_off6 + 2;
|
||||||
|
const tcp6_seq_off: usize = l4_off6 + 4;
|
||||||
|
const tcp6_ack_off: usize = l4_off6 + 8;
|
||||||
|
const tcp6_checksum_off: usize = l4_off6 + 16;
|
||||||
|
|
||||||
|
pub const SynTemplate6 = struct {
|
||||||
|
pub const max_frame_len: usize = opts_off6 + packet.max_syn_options_len;
|
||||||
|
|
||||||
|
base: [max_frame_len]u8,
|
||||||
|
frame_len: usize,
|
||||||
|
src_ip: [16]u8,
|
||||||
|
src_port: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
scan: packet.ScanType,
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
src_mac: [6]u8,
|
||||||
|
dst_mac: [6]u8,
|
||||||
|
src_ip: [16]u8,
|
||||||
|
src_port: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
profile: packet.OsProfile = .none,
|
||||||
|
scan: packet.ScanType = .syn,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init(cfg: Config) SynTemplate6 {
|
||||||
|
const opts = cfg.profile.options();
|
||||||
|
const tcp_total = tcp_len + opts.len;
|
||||||
|
const data_off_words: u8 = @intCast(tcp_total / 4);
|
||||||
|
|
||||||
|
var base: [max_frame_len]u8 = [_]u8{0} ** max_frame_len;
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = cfg.dst_mac,
|
||||||
|
.src = cfg.src_mac,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, ethertype_ipv6),
|
||||||
|
};
|
||||||
|
@memcpy(base[0..eth_len], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
const ip6 = packet.Ipv6Hdr{
|
||||||
|
.version_tc_flow = std.mem.nativeToBig(u32, ip6_version_tc_flow),
|
||||||
|
.payload_len = std.mem.nativeToBig(u16, @intCast(tcp_total)),
|
||||||
|
.next_header = ip6_next_tcp,
|
||||||
|
.hop_limit = default_hop_limit,
|
||||||
|
.src = cfg.src_ip,
|
||||||
|
.dst = [_]u8{0} ** 16,
|
||||||
|
};
|
||||||
|
@memcpy(base[eth_len..l4_off6], std.mem.asBytes(&ip6));
|
||||||
|
|
||||||
|
const tcp = packet.TcpHdr{
|
||||||
|
.src_port = std.mem.nativeToBig(u16, cfg.src_port),
|
||||||
|
.dst_port = 0,
|
||||||
|
.seq = 0,
|
||||||
|
.ack = 0,
|
||||||
|
.data_off_ns = data_off_words << 4,
|
||||||
|
.flags = cfg.scan.probeFlags(),
|
||||||
|
.window = std.mem.nativeToBig(u16, cfg.profile.window()),
|
||||||
|
.checksum = 0,
|
||||||
|
.urgent = 0,
|
||||||
|
};
|
||||||
|
@memcpy(base[l4_off6..opts_off6], std.mem.asBytes(&tcp));
|
||||||
|
if (opts.len > 0) @memcpy(base[opts_off6 .. opts_off6 + opts.len], opts);
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.base = base,
|
||||||
|
.frame_len = opts_off6 + opts.len,
|
||||||
|
.src_ip = cfg.src_ip,
|
||||||
|
.src_port = cfg.src_port,
|
||||||
|
.cookie = cfg.cookie,
|
||||||
|
.scan = cfg.scan,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stamp(self: *const SynTemplate6, out: *[max_frame_len]u8, dst_ip: [16]u8, dst_port: u16) usize {
|
||||||
|
const n = self.frame_len;
|
||||||
|
@memcpy(out[0..n], self.base[0..n]);
|
||||||
|
|
||||||
|
@memcpy(out[ip6_dst_off .. ip6_dst_off + 16], &dst_ip);
|
||||||
|
std.mem.writeInt(u16, out[tcp6_dst_off..][0..2], dst_port, .big);
|
||||||
|
|
||||||
|
const ck = self.cookie.seq6(dst_ip, dst_port, self.src_ip, self.src_port);
|
||||||
|
if (self.scan.cookieInAck()) {
|
||||||
|
std.mem.writeInt(u32, out[tcp6_seq_off..][0..4], 0, .big);
|
||||||
|
std.mem.writeInt(u32, out[tcp6_ack_off..][0..4], ck, .big);
|
||||||
|
} else {
|
||||||
|
std.mem.writeInt(u32, out[tcp6_seq_off..][0..4], ck, .big);
|
||||||
|
std.mem.writeInt(u32, out[tcp6_ack_off..][0..4], 0, .big);
|
||||||
|
}
|
||||||
|
|
||||||
|
std.mem.writeInt(u16, out[tcp6_checksum_off..][0..2], 0, .big);
|
||||||
|
const tcp_ck = packet.tcpChecksum6(self.src_ip, dst_ip, out[l4_off6..n]);
|
||||||
|
std.mem.writeInt(u16, out[tcp6_checksum_off..][0..2], tcp_ck, .big);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const v6_src = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
|
||||||
|
const v6_dst = [16]u8{ 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x99 };
|
||||||
|
|
||||||
|
fn testTemplate6(profile: packet.OsProfile) SynTemplate6 {
|
||||||
|
return SynTemplate6.init(.{
|
||||||
|
.src_mac = .{ 0x02, 0, 0, 0, 0, 1 },
|
||||||
|
.dst_mac = .{ 0x02, 0, 0, 0, 0, 2 },
|
||||||
|
.src_ip = v6_src,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.profile = profile,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the bare IPv6 SYN template stamps an 74-byte frame with a self-verifying TCP checksum" {
|
||||||
|
const tmpl = testTemplate6(.none);
|
||||||
|
var frame: [SynTemplate6.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, v6_dst, 443);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 14 + 40 + 20), len);
|
||||||
|
try std.testing.expectEqual(@as(u16, ethertype_ipv6), std.mem.readInt(u16, frame[12..14], .big));
|
||||||
|
try std.testing.expectEqual(@as(u8, 0x60), frame[14] & 0xf0);
|
||||||
|
try std.testing.expectEqual(ip6_next_tcp, frame[14 + 6]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 20), std.mem.readInt(u16, frame[ip6_payload_len_off..][0..2], .big));
|
||||||
|
try std.testing.expectEqualSlices(u8, &v6_dst, frame[ip6_dst_off .. ip6_dst_off + 16]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum6(v6_src, v6_dst, frame[l4_off6..len]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the IPv6 SYN carries the SipHash6 seq and the Linux option chain self-verifies" {
|
||||||
|
const tmpl = testTemplate6(.linux);
|
||||||
|
var frame: [SynTemplate6.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, v6_dst, 22);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 14 + 40 + 20 + 20), len);
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const want_seq = ck.seq6(v6_dst, 22, v6_src, 40000);
|
||||||
|
try std.testing.expectEqual(want_seq, std.mem.readInt(u32, frame[tcp6_seq_off..][0..4], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 40), std.mem.readInt(u16, frame[ip6_payload_len_off..][0..2], .big));
|
||||||
|
try std.testing.expectEqualSlices(u8, packet.OsProfile.linux.options(), frame[opts_off6..len]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum6(v6_src, v6_dst, frame[l4_off6..len]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "two IPv6 targets produce two different seqs" {
|
||||||
|
const tmpl = testTemplate6(.none);
|
||||||
|
var a: [SynTemplate6.max_frame_len]u8 = undefined;
|
||||||
|
var b: [SynTemplate6.max_frame_len]u8 = undefined;
|
||||||
|
var dst2 = v6_dst;
|
||||||
|
dst2[15] = 0x98;
|
||||||
|
_ = tmpl.stamp(&a, v6_dst, 443);
|
||||||
|
_ = tmpl.stamp(&b, dst2, 443);
|
||||||
|
try std.testing.expect(!std.mem.eql(u8, a[tcp6_seq_off..][0..4], b[tcp6_seq_off..][0..4]));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn testTemplate(profile: packet.OsProfile, scan: packet.ScanType) SynTemplate {
|
||||||
|
return SynTemplate.init(.{
|
||||||
|
.src_mac = .{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 },
|
||||||
|
.dst_mac = .{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x02 },
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.profile = profile,
|
||||||
|
.scan = scan,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the bare profile stamps a 54-byte frame with self-verifying IP and TCP checksums" {
|
||||||
|
const tmpl = testTemplate(.none, .syn);
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, 0x08080808, 443);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 54), len);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(frame[14..34]));
|
||||||
|
const ip_src = std.mem.nativeToBig(u32, 0x0a000001);
|
||||||
|
const ip_dst = std.mem.nativeToBig(u32, 0x08080808);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum(ip_src, ip_dst, frame[34..len]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stamp writes the destination and the SipHash seq" {
|
||||||
|
const tmpl = testTemplate(.none, .syn);
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&frame, 0x08080808, 443);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x08080808), std.mem.readInt(u32, frame[30..34], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 443), std.mem.readInt(u16, frame[36..38], .big));
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const want_seq = ck.seq(0x08080808, 443, 0x0a000001, 40000);
|
||||||
|
try std.testing.expectEqual(want_seq, std.mem.readInt(u32, frame[38..42], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "two different targets produce two different seqs" {
|
||||||
|
const tmpl = testTemplate(.none, .syn);
|
||||||
|
var a: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var b: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&a, 0x08080808, 443);
|
||||||
|
_ = tmpl.stamp(&b, 0x08080808, 80);
|
||||||
|
try std.testing.expect(!std.mem.eql(u8, a[38..42], b[38..42]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the Linux profile stamps a 74-byte frame carrying the option chain, checksums self-verify" {
|
||||||
|
const tmpl = testTemplate(.linux, .syn);
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, 0x08080808, 443);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(usize, 54 + 20), len);
|
||||||
|
try std.testing.expectEqual(@as(u8, 0xa0), frame[tcp_data_off_off]);
|
||||||
|
try std.testing.expectEqual(@as(u16, 60), std.mem.readInt(u16, frame[16..18], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 64240), std.mem.readInt(u16, frame[tcp_window_off..][0..2], .big));
|
||||||
|
try std.testing.expectEqualSlices(u8, packet.OsProfile.linux.options(), frame[54..len]);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(frame[14..34]));
|
||||||
|
const ip_src = std.mem.nativeToBig(u32, 0x0a000001);
|
||||||
|
const ip_dst = std.mem.nativeToBig(u32, 0x08080808);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum(ip_src, ip_dst, frame[34..len]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "windows and macos profiles produce well-formed variable-length frames" {
|
||||||
|
inline for (.{
|
||||||
|
.{ packet.OsProfile.windows, @as(usize, 54 + 12), @as(u8, 0x80) },
|
||||||
|
.{ packet.OsProfile.macos, @as(usize, 54 + 24), @as(u8, 0xb0) },
|
||||||
|
}) |case| {
|
||||||
|
const tmpl = testTemplate(case[0], .syn);
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, 0x01020304, 22);
|
||||||
|
try std.testing.expectEqual(case[1], len);
|
||||||
|
try std.testing.expectEqual(case[2], frame[tcp_data_off_off]);
|
||||||
|
try std.testing.expectEqual(@as(u16, @intCast(len - 14)), std.mem.readInt(u16, frame[16..18], .big));
|
||||||
|
const ip_src = std.mem.nativeToBig(u32, 0x0a000001);
|
||||||
|
const ip_dst = std.mem.nativeToBig(u32, 0x01020304);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(frame[14..34]));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum(ip_src, ip_dst, frame[34..len]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test "the SYN flag byte follows the scan type" {
|
||||||
|
const flags_off = tcp_flags_off;
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = testTemplate(.none, .fin).stamp(&frame, 0x08080808, 80);
|
||||||
|
try std.testing.expectEqual(packet.TcpFlag.fin, frame[flags_off]);
|
||||||
|
_ = testTemplate(.none, .xmas).stamp(&frame, 0x08080808, 80);
|
||||||
|
try std.testing.expectEqual(packet.TcpFlag.fin | packet.TcpFlag.psh | packet.TcpFlag.urg, frame[flags_off]);
|
||||||
|
_ = testTemplate(.none, .null_scan).stamp(&frame, 0x08080808, 80);
|
||||||
|
try std.testing.expectEqual(@as(u8, 0), frame[flags_off]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an ack-flag scan carries the cookie in the ack field and leaves seq zero" {
|
||||||
|
const tmpl = testTemplate(.none, .ack);
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&frame, 0x08080808, 80);
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const want = ck.seq(0x08080808, 80, 0x0a000001, 40000);
|
||||||
|
try std.testing.expectEqual(want, std.mem.readInt(u32, frame[42..46], .big));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0), std.mem.readInt(u32, frame[38..42], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "source-port rotation stays in range and keeps the seq cookie consistent with the written port" {
|
||||||
|
const tmpl = SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.rotate = true,
|
||||||
|
.rotate_base = 40000,
|
||||||
|
.rotate_span = 8192,
|
||||||
|
});
|
||||||
|
var frame: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&frame, 0x08080808, 443);
|
||||||
|
|
||||||
|
const sport = std.mem.readInt(u16, frame[34..36], .big);
|
||||||
|
try std.testing.expect(sport >= 40000 and sport < 48192);
|
||||||
|
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const want_seq = ck.seq(0x08080808, 443, 0x0a000001, sport);
|
||||||
|
try std.testing.expectEqual(want_seq, std.mem.readInt(u32, frame[38..42], .big));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "two different targets rotate to two different source ports" {
|
||||||
|
const tmpl = SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.rotate = true,
|
||||||
|
.rotate_base = 40000,
|
||||||
|
.rotate_span = 8192,
|
||||||
|
});
|
||||||
|
var a: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var b: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&a, 0x08080808, 443);
|
||||||
|
_ = tmpl.stamp(&b, 0x09090909, 443);
|
||||||
|
try std.testing.expect(!std.mem.eql(u8, a[34..36], b[34..36]));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "variantCount counts the real probe plus every decoy" {
|
||||||
|
const decoys = [_]u32{ 0x01010101, 0x02020202, 0x03030303 };
|
||||||
|
const tmpl = SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.decoys = &decoys,
|
||||||
|
});
|
||||||
|
try std.testing.expectEqual(@as(usize, 4), tmpl.variantCount());
|
||||||
|
const plain = testTemplate(.none, .syn);
|
||||||
|
try std.testing.expectEqual(@as(usize, 1), plain.variantCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "decoy variants carry a spoofed source with self-verifying IP and TCP checksums" {
|
||||||
|
const decoys = [_]u32{ 0xC0A80063, 0x08080404 };
|
||||||
|
const tmpl = SynTemplate.init(.{
|
||||||
|
.src_mac = .{ 0x02, 0, 0, 0, 0, 1 },
|
||||||
|
.dst_mac = .{ 0x02, 0, 0, 0, 0, 2 },
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.decoys = &decoys,
|
||||||
|
});
|
||||||
|
var real: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var decoy: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
|
||||||
|
const rn = tmpl.stampVariant(&real, 0x08080808, 443, 0);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0x0a000001), std.mem.readInt(u32, real[26..30], .big));
|
||||||
|
|
||||||
|
const dn = tmpl.stampVariant(&decoy, 0x08080808, 443, 1);
|
||||||
|
try std.testing.expectEqual(rn, dn);
|
||||||
|
try std.testing.expectEqual(@as(u32, 0xC0A80063), std.mem.readInt(u32, decoy[26..30], .big));
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(decoy[14..34]));
|
||||||
|
const decoy_src_be = std.mem.nativeToBig(u32, 0xC0A80063);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, 0x08080808);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum(decoy_src_be, dst_be, decoy[34..dn]));
|
||||||
|
|
||||||
|
try std.testing.expect(!std.mem.eql(u8, real[26..30], decoy[26..30]));
|
||||||
|
try std.testing.expectEqualSlices(u8, real[30..34], decoy[30..34]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "windows and macos profiles vary the IP id per target while linux and bare keep it zero" {
|
||||||
|
var a: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var b: [SynTemplate.max_frame_len]u8 = undefined;
|
||||||
|
|
||||||
|
inline for (.{ packet.OsProfile.windows, packet.OsProfile.macos }) |p| {
|
||||||
|
const tmpl = testTemplate(p, .syn);
|
||||||
|
_ = tmpl.stamp(&a, 0x08080808, 443);
|
||||||
|
_ = tmpl.stamp(&b, 0x09090909, 443);
|
||||||
|
const id_a = std.mem.readInt(u16, a[18..20], .big);
|
||||||
|
const id_b = std.mem.readInt(u16, b[18..20], .big);
|
||||||
|
try std.testing.expect(id_a != id_b);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(a[14..34]));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = testTemplate(.linux, .syn).stamp(&a, 0x08080808, 443);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), std.mem.readInt(u16, a[18..20], .big));
|
||||||
|
_ = testTemplate(.none, .syn).stamp(&a, 0x08080808, 443);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), std.mem.readInt(u16, a[18..20], .big));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,470 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// tx.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const targets = @import("targets");
|
||||||
|
const template = @import("template");
|
||||||
|
const ratelimit = @import("ratelimit");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const packet = @import("packet");
|
||||||
|
|
||||||
|
fn submitFrame(sink: anytype, frame: []const u8) bool {
|
||||||
|
if (!sink.submit(frame)) {
|
||||||
|
@branchHint(.unlikely);
|
||||||
|
sink.kick();
|
||||||
|
return sink.submit(frame);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(
|
||||||
|
engine: *targets.Engine,
|
||||||
|
tmpl: anytype,
|
||||||
|
bucket: *ratelimit.TokenBucket,
|
||||||
|
sink: anytype,
|
||||||
|
clock: anytype,
|
||||||
|
max_packets: u64,
|
||||||
|
deadline_ns: u64,
|
||||||
|
) u64 {
|
||||||
|
if (bucket.jitter != null) return runJittered(engine, tmpl, bucket, sink, clock, max_packets, deadline_ns);
|
||||||
|
|
||||||
|
bucket.prime(clock.now());
|
||||||
|
var sent: u64 = 0;
|
||||||
|
var probes: u64 = 0;
|
||||||
|
var cursor: usize = 0;
|
||||||
|
var frame: [@TypeOf(tmpl.*).max_frame_len]u8 = undefined;
|
||||||
|
const vc = tmpl.variantCount();
|
||||||
|
|
||||||
|
var pending: ?targets.Target = engine.next();
|
||||||
|
while (pending != null and probes < max_packets) {
|
||||||
|
const now_ns = clock.now();
|
||||||
|
if (now_ns >= deadline_ns) break;
|
||||||
|
const granted = bucket.takeBatch(now_ns, (max_packets - probes) *| vc);
|
||||||
|
if (granted == 0) {
|
||||||
|
clock.sleepNs(bucket.step_ns);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var used: u64 = 0;
|
||||||
|
while (used < granted and probes < max_packets) {
|
||||||
|
const t = pending orelse break;
|
||||||
|
const len = tmpl.stampVariant(&frame, t.ip, t.port, cursor);
|
||||||
|
if (!submitFrame(sink, frame[0..len])) break;
|
||||||
|
used += 1;
|
||||||
|
sent += 1;
|
||||||
|
cursor += 1;
|
||||||
|
if (cursor == vc) {
|
||||||
|
cursor = 0;
|
||||||
|
probes += 1;
|
||||||
|
pending = engine.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (used < granted) bucket.refund(granted - used);
|
||||||
|
sink.kick();
|
||||||
|
}
|
||||||
|
sink.kick();
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runJittered(
|
||||||
|
engine: *targets.Engine,
|
||||||
|
tmpl: anytype,
|
||||||
|
bucket: *ratelimit.TokenBucket,
|
||||||
|
sink: anytype,
|
||||||
|
clock: anytype,
|
||||||
|
max_packets: u64,
|
||||||
|
deadline_ns: u64,
|
||||||
|
) u64 {
|
||||||
|
var sent: u64 = 0;
|
||||||
|
var probes: u64 = 0;
|
||||||
|
var cursor: usize = 0;
|
||||||
|
var frame: [@TypeOf(tmpl.*).max_frame_len]u8 = undefined;
|
||||||
|
const vc = tmpl.variantCount();
|
||||||
|
|
||||||
|
var pending: ?targets.Target = engine.next();
|
||||||
|
while (pending != null and probes < max_packets) {
|
||||||
|
if (clock.now() >= deadline_ns) break;
|
||||||
|
const t = pending.?;
|
||||||
|
const len = tmpl.stampVariant(&frame, t.ip, t.port, cursor);
|
||||||
|
if (!submitFrame(sink, frame[0..len])) {
|
||||||
|
clock.sleepNs(bucket.step_ns);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sent += 1;
|
||||||
|
cursor += 1;
|
||||||
|
sink.kick();
|
||||||
|
if (cursor < vc) continue;
|
||||||
|
cursor = 0;
|
||||||
|
probes += 1;
|
||||||
|
pending = engine.next();
|
||||||
|
if (pending == null) break;
|
||||||
|
clock.sleepNs(bucket.jitter.?.nextGapNs() *| vc);
|
||||||
|
}
|
||||||
|
sink.kick();
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn runV6(
|
||||||
|
engine: *targets.Engine6,
|
||||||
|
tmpl: anytype,
|
||||||
|
bucket: *ratelimit.TokenBucket,
|
||||||
|
sink: anytype,
|
||||||
|
clock: anytype,
|
||||||
|
max_packets: u64,
|
||||||
|
deadline_ns: u64,
|
||||||
|
) u64 {
|
||||||
|
bucket.prime(clock.now());
|
||||||
|
var sent: u64 = 0;
|
||||||
|
var frame: [@TypeOf(tmpl.*).max_frame_len]u8 = undefined;
|
||||||
|
|
||||||
|
var pending: ?targets.Target6 = engine.next();
|
||||||
|
while (pending != null and sent < max_packets) {
|
||||||
|
const now_ns = clock.now();
|
||||||
|
if (now_ns >= deadline_ns) break;
|
||||||
|
const granted = bucket.takeBatch(now_ns, max_packets - sent);
|
||||||
|
if (granted == 0) {
|
||||||
|
clock.sleepNs(bucket.step_ns);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
var used: u64 = 0;
|
||||||
|
while (used < granted and sent < max_packets) {
|
||||||
|
const t = pending orelse break;
|
||||||
|
const len = tmpl.stamp(&frame, t.addr, t.port);
|
||||||
|
if (!submitFrame(sink, frame[0..len])) break;
|
||||||
|
used += 1;
|
||||||
|
sent += 1;
|
||||||
|
pending = engine.next();
|
||||||
|
}
|
||||||
|
if (used < granted) bucket.refund(granted - used);
|
||||||
|
sink.kick();
|
||||||
|
}
|
||||||
|
sink.kick();
|
||||||
|
return sent;
|
||||||
|
}
|
||||||
|
|
||||||
|
const FakeClock = struct {
|
||||||
|
t: u64 = 0,
|
||||||
|
fn now(self: *FakeClock) u64 {
|
||||||
|
self.t += 1_000_000_000;
|
||||||
|
return self.t;
|
||||||
|
}
|
||||||
|
fn sleepNs(self: *FakeClock, ns: u64) void {
|
||||||
|
self.t += ns;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const FakeSink = struct {
|
||||||
|
frames: std.ArrayList([54]u8) = .empty,
|
||||||
|
kicks: usize = 0,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
fn submit(self: *FakeSink, frame: []const u8) bool {
|
||||||
|
self.frames.append(self.allocator, frame[0..54].*) catch return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
fn kick(self: *FakeSink) void {
|
||||||
|
self.kicks += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "the TX engine drives the M2 bijection through stamp + ratelimit + submit" {
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/30")};
|
||||||
|
const ports = [_]u16{ 80, 443 };
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 0xDEADBEEF);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 64);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = FakeSink{ .allocator = std.testing.allocator };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, 1_000_000, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(@as(u64, 8), sent);
|
||||||
|
try std.testing.expectEqual(@as(usize, 8), sink.frames.items.len);
|
||||||
|
try std.testing.expect(sink.kicks >= 1);
|
||||||
|
|
||||||
|
var seen = std.AutoHashMap(u64, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
for (sink.frames.items) |*f| {
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(f[14..34]));
|
||||||
|
const ip = std.mem.readInt(u32, f[30..34], .big);
|
||||||
|
const port = std.mem.readInt(u16, f[36..38], .big);
|
||||||
|
try std.testing.expect(!targets.isReserved(ip));
|
||||||
|
const key = (@as(u64, ip) << 16) | port;
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, 8), seen.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "max_packets caps the send count below the target total" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/28")};
|
||||||
|
const ports = [_]u16{ 80, 443, 22 };
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 0x1234);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 64);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = FakeSink{ .allocator = std.testing.allocator };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, 5, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(@as(u64, 5), sent);
|
||||||
|
try std.testing.expectEqual(@as(usize, 5), sink.frames.items.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
const StuckSink = struct {
|
||||||
|
kicks: usize = 0,
|
||||||
|
fn submit(_: *StuckSink, _: []const u8) bool {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fn kick(self: *StuckSink) void {
|
||||||
|
self.kicks += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "run bails at the deadline when the sink never drains (stall watchdog)" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/28")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 0x1234);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 64);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = StuckSink{};
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, 1_000_000, 5_000_000_000);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), sent);
|
||||||
|
try std.testing.expect(sink.kicks >= 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "decoys emit the real probe plus every spoofed source for each target" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/30")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 9);
|
||||||
|
defer eng.deinit();
|
||||||
|
const total = eng.total;
|
||||||
|
|
||||||
|
const decoys = [_]u32{ 0x01010101, 0x02020202 };
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.decoys = &decoys,
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 1000);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = FakeSink{ .allocator = std.testing.allocator };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, total, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(total * 3, sent);
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total * 3)), sink.frames.items.len);
|
||||||
|
|
||||||
|
var real_count: usize = 0;
|
||||||
|
for (sink.frames.items) |*f| {
|
||||||
|
if (std.mem.readInt(u32, f[26..30], .big) == 0x0a000001) real_count += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total)), real_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "jittered pacing covers every target and advances the clock by the sleeps" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/29")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 4);
|
||||||
|
defer eng.deinit();
|
||||||
|
const total = eng.total;
|
||||||
|
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 1000).withJitter(0xC0FFEE);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = FakeSink{ .allocator = std.testing.allocator };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const before = clock.t;
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, total, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(total, sent);
|
||||||
|
try std.testing.expect(clock.t > before);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SlowClock = struct {
|
||||||
|
t: u64 = 0,
|
||||||
|
fn now(self: *SlowClock) u64 {
|
||||||
|
self.t += 1_000;
|
||||||
|
return self.t;
|
||||||
|
}
|
||||||
|
fn sleepNs(self: *SlowClock, ns: u64) void {
|
||||||
|
self.t += ns;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const CountSink = struct {
|
||||||
|
count: u64 = 0,
|
||||||
|
fn submit(self: *CountSink, _: []const u8) bool {
|
||||||
|
self.count += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
fn kick(_: *CountSink) void {}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "decoy scans keep making progress past the initial token burst (no livelock)" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.0.0/20")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 11);
|
||||||
|
defer eng.deinit();
|
||||||
|
const total = eng.total;
|
||||||
|
|
||||||
|
const decoys = [_]u32{ 0x01010101, 0x02020202 };
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.decoys = &decoys,
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 1000);
|
||||||
|
var clock = SlowClock{};
|
||||||
|
var sink = CountSink{};
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, total, 6_000_000_000);
|
||||||
|
try std.testing.expectEqual(sink.count, sent);
|
||||||
|
try std.testing.expect(sent > 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
const BoundedSink = struct {
|
||||||
|
frames: std.ArrayList([54]u8) = .empty,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
held: usize = 0,
|
||||||
|
cap: usize,
|
||||||
|
fn submit(self: *BoundedSink, frame: []const u8) bool {
|
||||||
|
if (self.held >= self.cap) return false;
|
||||||
|
self.frames.append(self.allocator, frame[0..54].*) catch return false;
|
||||||
|
self.held += 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
fn kick(self: *BoundedSink) void {
|
||||||
|
self.held = 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const V6Sink = struct {
|
||||||
|
frames: std.ArrayList([74]u8) = .empty,
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
fn submit(self: *V6Sink, frame: []const u8) bool {
|
||||||
|
self.frames.append(self.allocator, frame[0..74].*) catch return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
fn kick(_: *V6Sink) void {}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "the IPv6 TX engine drives Engine6 through stamp6 with self-verifying checksums and a bijection" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidr = try targets.parseCidr6("2001:470:1:2::/122");
|
||||||
|
const ports = [_]u16{ 80, 443 };
|
||||||
|
var eng = try targets.Engine6.init(std.testing.allocator, cidr, &ports, 0xBEEF, targets.default_max_hosts6);
|
||||||
|
defer eng.deinit();
|
||||||
|
const total = eng.total;
|
||||||
|
|
||||||
|
const src_ip = try @import("netutil").parseIpv6("2001:470:1:2::1");
|
||||||
|
const tmpl = template.SynTemplate6.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = src_ip,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 1000);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = V6Sink{ .allocator = std.testing.allocator };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const sent = runV6(&eng, &tmpl, &tb, &sink, &clock, total, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(total, sent);
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total)), sink.frames.items.len);
|
||||||
|
|
||||||
|
var seen = std.AutoHashMap([18]u8, void).init(std.testing.allocator);
|
||||||
|
defer seen.deinit();
|
||||||
|
for (sink.frames.items) |*f| {
|
||||||
|
try std.testing.expectEqual(@as(u16, 0x86dd), std.mem.readInt(u16, f[12..14], .big));
|
||||||
|
var dst: [16]u8 = undefined;
|
||||||
|
@memcpy(&dst, f[38..54]);
|
||||||
|
try std.testing.expect(!targets.isReserved6(dst));
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.tcpChecksum6(src_ip, dst, f[54..74]));
|
||||||
|
var key: [18]u8 = undefined;
|
||||||
|
@memcpy(key[0..16], &dst);
|
||||||
|
@memcpy(key[16..18], f[56..58]);
|
||||||
|
try std.testing.expect(!seen.contains(key));
|
||||||
|
try seen.put(key, {});
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total)), seen.count());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "decoy groups resume across backpressure without re-sending the real probe" {
|
||||||
|
const test_key = [_]u8{0} ** 16;
|
||||||
|
const cidrs = [_]targets.Range{try targets.parseCidr("8.8.8.0/29")};
|
||||||
|
const ports = [_]u16{80};
|
||||||
|
var eng = try targets.Engine.init(std.testing.allocator, &cidrs, &ports, 13);
|
||||||
|
defer eng.deinit();
|
||||||
|
const total = eng.total;
|
||||||
|
|
||||||
|
const decoys = [_]u32{ 0x01010101, 0x02020202 };
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = .{0} ** 6,
|
||||||
|
.dst_mac = .{0} ** 6,
|
||||||
|
.src_ip = 0x0a000001,
|
||||||
|
.src_port = 40000,
|
||||||
|
.cookie = cookie.Cookie.init(test_key),
|
||||||
|
.decoys = &decoys,
|
||||||
|
});
|
||||||
|
var tb = ratelimit.TokenBucket.init(1000, 1000);
|
||||||
|
var clock = FakeClock{};
|
||||||
|
var sink = BoundedSink{ .allocator = std.testing.allocator, .cap = 2 };
|
||||||
|
defer sink.frames.deinit(std.testing.allocator);
|
||||||
|
|
||||||
|
const sent = run(&eng, &tmpl, &tb, &sink, &clock, total, std.math.maxInt(u64));
|
||||||
|
try std.testing.expectEqual(total * 3, sent);
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total * 3)), sink.frames.items.len);
|
||||||
|
|
||||||
|
var real: usize = 0;
|
||||||
|
for (sink.frames.items) |*f| {
|
||||||
|
if (std.mem.readInt(u32, f[26..30], .big) == 0x0a000001) real += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(@as(usize, @intCast(total)), real);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,154 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// txcmd.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const targets = @import("targets");
|
||||||
|
const template = @import("template");
|
||||||
|
const ratelimit = @import("ratelimit");
|
||||||
|
const packet_io = @import("packet_io");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const tx = @import("tx");
|
||||||
|
const netutil = @import("netutil");
|
||||||
|
const stealth = @import("stealth");
|
||||||
|
|
||||||
|
const authorized_warning =
|
||||||
|
"tx: stealth/evasion features require --authorized-scan. Use ONLY on systems you\n" ++
|
||||||
|
"own or are authorized to test; unauthorized scanning is a crime (CFAA et al.).\n\n" ++
|
||||||
|
stealth.omitted_help ++ "\n";
|
||||||
|
|
||||||
|
const getFlag = netutil.getFlag;
|
||||||
|
const parseIpv4 = netutil.parseIpv4;
|
||||||
|
const parseMac = netutil.parseMac;
|
||||||
|
const parsePorts = netutil.parsePorts;
|
||||||
|
const resolveSrcIp = netutil.resolveSrcIp;
|
||||||
|
const resolveSrcMac = netutil.resolveSrcMac;
|
||||||
|
const RealClock = netutil.RealClock;
|
||||||
|
|
||||||
|
const default_iface = "lo";
|
||||||
|
const default_rate: u64 = 10_000;
|
||||||
|
const default_src_port: u16 = 40_000;
|
||||||
|
const default_ports = [_]u16{80};
|
||||||
|
const ns_per_sec: u64 = 1_000_000_000;
|
||||||
|
const tx_budget_floor_ns: u64 = 60 * ns_per_sec;
|
||||||
|
|
||||||
|
pub fn run(io: std.Io, allocator: std.mem.Allocator, args: []const []const u8) !void {
|
||||||
|
var buf: [512]u8 = undefined;
|
||||||
|
var fw = std.Io.File.stdout().writer(io, &buf);
|
||||||
|
const out = &fw.interface;
|
||||||
|
|
||||||
|
const target_text = getFlag(args, "--target") orelse {
|
||||||
|
try out.writeAll("tx: --target <cidr> is required (e.g. --target 10.0.0.0/24)\n");
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
const ifname = getFlag(args, "--iface") orelse default_iface;
|
||||||
|
const rate = if (getFlag(args, "--rate")) |r| try std.fmt.parseInt(u64, r, 10) else default_rate;
|
||||||
|
const src_port = if (getFlag(args, "--src-port")) |p| try std.fmt.parseInt(u16, p, 10) else default_src_port;
|
||||||
|
|
||||||
|
const ports = if (getFlag(args, "--ports")) |p| try parsePorts(allocator, p) else try allocator.dupe(u16, &default_ports);
|
||||||
|
const gw_mac = if (getFlag(args, "--gw-mac")) |m| try parseMac(m) else [_]u8{0} ** 6;
|
||||||
|
const src_ip = if (getFlag(args, "--src-ip")) |s| try parseIpv4(s) else try resolveSrcIp(ifname);
|
||||||
|
const src_mac = try resolveSrcMac(ifname);
|
||||||
|
|
||||||
|
var seed: u64 = undefined;
|
||||||
|
if (getFlag(args, "--seed")) |s| {
|
||||||
|
seed = try std.fmt.parseInt(u64, s, 10);
|
||||||
|
} else {
|
||||||
|
var seed_bytes: [8]u8 = undefined;
|
||||||
|
try io.randomSecure(&seed_bytes);
|
||||||
|
seed = std.mem.readInt(u64, &seed_bytes, .little);
|
||||||
|
}
|
||||||
|
|
||||||
|
var scfg = stealth.parse(allocator, io, args) catch |e| switch (e) {
|
||||||
|
error.AuthorizationRequired => {
|
||||||
|
try out.writeAll(authorized_warning);
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.OutOfMemory => return e,
|
||||||
|
else => {
|
||||||
|
try out.print("tx: invalid stealth flag ({s})\n", .{@errorName(e)});
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
defer scfg.deinit(allocator);
|
||||||
|
|
||||||
|
const cidr = try targets.parseCidr(target_text);
|
||||||
|
var eng = try targets.Engine.init(allocator, &.{cidr}, ports, seed);
|
||||||
|
defer eng.deinit();
|
||||||
|
|
||||||
|
const count = if (getFlag(args, "--count")) |c| try std.fmt.parseInt(u64, c, 10) else eng.total;
|
||||||
|
|
||||||
|
const ck = try cookie.Cookie.random(io);
|
||||||
|
const rot_span: u16 = if (scfg.rotate) @intCast(@min(@as(u32, scfg.rotate_span), 65536 - @as(u32, src_port))) else 0;
|
||||||
|
const tmpl = template.SynTemplate.init(.{
|
||||||
|
.src_mac = src_mac,
|
||||||
|
.dst_mac = gw_mac,
|
||||||
|
.src_ip = src_ip,
|
||||||
|
.src_port = src_port,
|
||||||
|
.cookie = ck,
|
||||||
|
.profile = scfg.profile,
|
||||||
|
.scan = scfg.scan,
|
||||||
|
.rotate = scfg.rotate,
|
||||||
|
.rotate_base = src_port,
|
||||||
|
.rotate_span = rot_span,
|
||||||
|
.decoys = scfg.decoys,
|
||||||
|
});
|
||||||
|
var bucket = ratelimit.TokenBucket.init(rate, rate);
|
||||||
|
if (scfg.jitter) bucket = bucket.withJitter(seed);
|
||||||
|
|
||||||
|
const backend_choice = packet_io.parseChoice(getFlag(args, "--backend")) orelse {
|
||||||
|
try out.writeAll("tx: --backend must be one of auto, xdp, afpacket\n");
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
var backend = packet_io.select(allocator, ifname, backend_choice, .{}, .{}, out) catch |err| switch (err) {
|
||||||
|
error.NeedCapNetRaw => {
|
||||||
|
try out.writeAll("tx: need CAP_NET_RAW + CAP_NET_ADMIN. Grant once, then re-run (no sudo):\n sudo setcap cap_net_raw,cap_net_admin=eip ./zig-out/bin/zingela\nSkipping.\n");
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
error.XdpNotCompiledIn => {
|
||||||
|
try out.writeAll("tx: --backend xdp needs a build with -Dxdp\n");
|
||||||
|
try out.flush();
|
||||||
|
return;
|
||||||
|
},
|
||||||
|
else => return err,
|
||||||
|
};
|
||||||
|
defer backend.close();
|
||||||
|
try out.print("tx: using {s}\n", .{packet_io.kindLabel(backend.kind())});
|
||||||
|
|
||||||
|
if (scfg.profile != .none or scfg.scan != .syn or scfg.jitter or scfg.rotate or scfg.decoys.len > 0) {
|
||||||
|
try out.print("tx: stealth template={s} scan={s} jitter={s} rotate={s} decoys={d}\n", .{
|
||||||
|
@tagName(scfg.profile),
|
||||||
|
@tagName(scfg.scan),
|
||||||
|
if (scfg.jitter) "on" else "off",
|
||||||
|
if (scfg.rotate) "on" else "off",
|
||||||
|
scfg.decoys.len,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var supp: ?stealth.RstSuppressor = null;
|
||||||
|
if (scfg.suppress_rst) {
|
||||||
|
const lo = src_port;
|
||||||
|
const hi = if (scfg.rotate) src_port +| (rot_span -| 1) else src_port;
|
||||||
|
supp = stealth.RstSuppressor.install(allocator, io, src_ip, lo, hi) catch |e| blk: {
|
||||||
|
try out.print("tx: RST-suppression unavailable ({s}); continuing without it\n", .{@errorName(e)});
|
||||||
|
break :blk null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
defer if (supp) |*s| s.teardown();
|
||||||
|
|
||||||
|
var clock = RealClock{};
|
||||||
|
const t0 = clock.now();
|
||||||
|
const est_tx_ns: u64 = if (rate > 0) (count / rate) *| ns_per_sec else tx_budget_floor_ns;
|
||||||
|
const deadline_ns = t0 +| (est_tx_ns *| 4) +| tx_budget_floor_ns;
|
||||||
|
const sent = tx.run(&eng, &tmpl, &bucket, &backend, &clock, count, deadline_ns);
|
||||||
|
const elapsed_ns = clock.now() - t0;
|
||||||
|
|
||||||
|
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / 1_000_000_000.0;
|
||||||
|
const pps = if (elapsed_s > 0) @as(f64, @floatFromInt(sent)) / elapsed_s else 0;
|
||||||
|
try out.print("tx: sent {d} SYN frames on {s} in {d:.3}s ({d:.0} pps)\n", .{ sent, ifname, elapsed_s, pps });
|
||||||
|
try out.flush();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,204 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// udp.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const packet = @import("packet");
|
||||||
|
const cookie = @import("cookie");
|
||||||
|
const payloads = @import("payloads");
|
||||||
|
|
||||||
|
const ethertype_ipv4: u16 = 0x0800;
|
||||||
|
const ipv4_version_ihl: u8 = 0x45;
|
||||||
|
const ip_proto_udp: u8 = 17;
|
||||||
|
const ip_flag_dont_fragment: u16 = 0x4000;
|
||||||
|
const default_ttl: u8 = 64;
|
||||||
|
|
||||||
|
const eth_len: usize = 14;
|
||||||
|
const ip_len: usize = 20;
|
||||||
|
const udp_len: usize = 8;
|
||||||
|
const l4_off: usize = eth_len + ip_len;
|
||||||
|
const payload_off: usize = l4_off + udp_len;
|
||||||
|
|
||||||
|
const ip_total_len_off: usize = eth_len + 2;
|
||||||
|
const ip_checksum_off: usize = eth_len + 10;
|
||||||
|
const ip_dst_off: usize = eth_len + 16;
|
||||||
|
const udp_src_off: usize = l4_off;
|
||||||
|
const udp_dst_off: usize = l4_off + 2;
|
||||||
|
const udp_len_off: usize = l4_off + 4;
|
||||||
|
const udp_checksum_off: usize = l4_off + 6;
|
||||||
|
|
||||||
|
pub const UdpTemplate = struct {
|
||||||
|
pub const max_frame_len: usize = payload_off + payloads.max_len;
|
||||||
|
|
||||||
|
base: [payload_off]u8,
|
||||||
|
src_ip_be: u32,
|
||||||
|
src_ip: u32,
|
||||||
|
src_port_base: u16,
|
||||||
|
src_port_span: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
|
||||||
|
pub const Config = struct {
|
||||||
|
src_mac: [6]u8,
|
||||||
|
dst_mac: [6]u8,
|
||||||
|
src_ip: u32,
|
||||||
|
src_port_base: u16,
|
||||||
|
src_port_span: u16,
|
||||||
|
cookie: cookie.Cookie,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init(cfg: Config) UdpTemplate {
|
||||||
|
var base: [payload_off]u8 = undefined;
|
||||||
|
|
||||||
|
const eth = packet.EthHdr{
|
||||||
|
.dst = cfg.dst_mac,
|
||||||
|
.src = cfg.src_mac,
|
||||||
|
.ethertype = std.mem.nativeToBig(u16, ethertype_ipv4),
|
||||||
|
};
|
||||||
|
@memcpy(base[0..eth_len], std.mem.asBytes(ð));
|
||||||
|
|
||||||
|
const ip = packet.Ipv4Hdr{
|
||||||
|
.version_ihl = ipv4_version_ihl,
|
||||||
|
.tos = 0,
|
||||||
|
.total_len = 0,
|
||||||
|
.id = 0,
|
||||||
|
.flags_frag = std.mem.nativeToBig(u16, ip_flag_dont_fragment),
|
||||||
|
.ttl = default_ttl,
|
||||||
|
.protocol = ip_proto_udp,
|
||||||
|
.checksum = 0,
|
||||||
|
.src = std.mem.nativeToBig(u32, cfg.src_ip),
|
||||||
|
.dst = 0,
|
||||||
|
};
|
||||||
|
@memcpy(base[eth_len..l4_off], std.mem.asBytes(&ip));
|
||||||
|
|
||||||
|
const udp = packet.UdpHdr{
|
||||||
|
.src_port = 0,
|
||||||
|
.dst_port = 0,
|
||||||
|
.length = 0,
|
||||||
|
.checksum = 0,
|
||||||
|
};
|
||||||
|
@memcpy(base[l4_off..payload_off], std.mem.asBytes(&udp));
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.base = base,
|
||||||
|
.src_ip_be = std.mem.nativeToBig(u32, cfg.src_ip),
|
||||||
|
.src_ip = cfg.src_ip,
|
||||||
|
.src_port_base = cfg.src_port_base,
|
||||||
|
.src_port_span = cfg.src_port_span,
|
||||||
|
.cookie = cfg.cookie,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stamp(self: *const UdpTemplate, out: *[max_frame_len]u8, dst_ip: u32, dst_port: u16) usize {
|
||||||
|
const payload = payloads.lookup(dst_port);
|
||||||
|
const udp_total = udp_len + payload.len;
|
||||||
|
const ip_total = ip_len + udp_total;
|
||||||
|
const frame_len = eth_len + ip_total;
|
||||||
|
|
||||||
|
@memcpy(out[0..payload_off], &self.base);
|
||||||
|
@memcpy(out[payload_off .. payload_off + payload.len], payload);
|
||||||
|
|
||||||
|
std.mem.writeInt(u16, out[ip_total_len_off..][0..2], @intCast(ip_total), .big);
|
||||||
|
std.mem.writeInt(u32, out[ip_dst_off..][0..4], dst_ip, .big);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], 0, .big);
|
||||||
|
const ip_ck = packet.checksum(out[eth_len..l4_off]);
|
||||||
|
std.mem.writeInt(u16, out[ip_checksum_off..][0..2], ip_ck, .big);
|
||||||
|
|
||||||
|
const src_port = self.cookie.udpSrcPort(dst_ip, dst_port, self.src_ip, self.src_port_base, self.src_port_span);
|
||||||
|
std.mem.writeInt(u16, out[udp_src_off..][0..2], src_port, .big);
|
||||||
|
std.mem.writeInt(u16, out[udp_dst_off..][0..2], dst_port, .big);
|
||||||
|
std.mem.writeInt(u16, out[udp_len_off..][0..2], @intCast(udp_total), .big);
|
||||||
|
std.mem.writeInt(u16, out[udp_checksum_off..][0..2], 0, .big);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, dst_ip);
|
||||||
|
const udp_ck = packet.udpChecksum(self.src_ip_be, dst_be, out[l4_off .. payload_off + payload.len]);
|
||||||
|
std.mem.writeInt(u16, out[udp_checksum_off..][0..2], udp_ck, .big);
|
||||||
|
|
||||||
|
return frame_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn variantCount(_: *const UdpTemplate) usize {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stampVariant(self: *const UdpTemplate, out: *[max_frame_len]u8, dst_ip: u32, dst_port: u16, variant: usize) usize {
|
||||||
|
_ = variant;
|
||||||
|
return self.stamp(out, dst_ip, dst_port);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const test_key = [16]u8{
|
||||||
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||||
|
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
||||||
|
};
|
||||||
|
|
||||||
|
const our_ip: u32 = 0x0a000001;
|
||||||
|
const their_ip: u32 = 0x08080808;
|
||||||
|
const base_port: u16 = 40000;
|
||||||
|
const span: u16 = 8192;
|
||||||
|
|
||||||
|
fn testTemplate(ck: cookie.Cookie) UdpTemplate {
|
||||||
|
return UdpTemplate.init(.{
|
||||||
|
.src_mac = .{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x01 },
|
||||||
|
.dst_mac = .{ 0x02, 0x00, 0x00, 0x00, 0x00, 0x02 },
|
||||||
|
.src_ip = our_ip,
|
||||||
|
.src_port_base = base_port,
|
||||||
|
.src_port_span = span,
|
||||||
|
.cookie = ck,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stamped UDP frame carries self-verifying IP and UDP checksums" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const tmpl = testTemplate(ck);
|
||||||
|
var frame: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
const len = tmpl.stamp(&frame, their_ip, 53);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u16, 0), packet.checksum(frame[eth_len..l4_off]));
|
||||||
|
const src_be = std.mem.nativeToBig(u32, our_ip);
|
||||||
|
const dst_be = std.mem.nativeToBig(u32, their_ip);
|
||||||
|
try std.testing.expectEqual(@as(u16, 0xffff), packet.udpChecksum(src_be, dst_be, frame[l4_off..len]));
|
||||||
|
try std.testing.expect(std.mem.readInt(u16, frame[udp_checksum_off..][0..2], .big) != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stamp selects the per-protocol payload and computes the frame length" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const tmpl = testTemplate(ck);
|
||||||
|
var frame: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
|
||||||
|
const known = payloads.lookup(53);
|
||||||
|
const len53 = tmpl.stamp(&frame, their_ip, 53);
|
||||||
|
try std.testing.expectEqual(payload_off + known.len, len53);
|
||||||
|
try std.testing.expectEqualSlices(u8, known, frame[payload_off..len53]);
|
||||||
|
try std.testing.expectEqual(@as(u16, @intCast(udp_len + known.len)), std.mem.readInt(u16, frame[udp_len_off..][0..2], .big));
|
||||||
|
|
||||||
|
const len80 = tmpl.stamp(&frame, their_ip, 80);
|
||||||
|
try std.testing.expectEqual(payload_off, len80);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stamp writes the cookie-derived UDP source port" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const tmpl = testTemplate(ck);
|
||||||
|
var frame: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&frame, their_ip, 53);
|
||||||
|
const want = ck.udpSrcPort(their_ip, 53, our_ip, base_port, span);
|
||||||
|
try std.testing.expectEqual(want, std.mem.readInt(u16, frame[udp_src_off..][0..2], .big));
|
||||||
|
try std.testing.expect(want >= base_port and want < base_port + span);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "stamp writes destination, port, and the UDP protocol number" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const tmpl = testTemplate(ck);
|
||||||
|
var frame: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&frame, their_ip, 161);
|
||||||
|
try std.testing.expectEqual(their_ip, std.mem.readInt(u32, frame[ip_dst_off..][0..4], .big));
|
||||||
|
try std.testing.expectEqual(@as(u16, 161), std.mem.readInt(u16, frame[udp_dst_off..][0..2], .big));
|
||||||
|
try std.testing.expectEqual(ip_proto_udp, frame[eth_len + 9]);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "two different target ports produce two different source ports" {
|
||||||
|
const ck = cookie.Cookie.init(test_key);
|
||||||
|
const tmpl = testTemplate(ck);
|
||||||
|
var a: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
var b: [UdpTemplate.max_frame_len]u8 = undefined;
|
||||||
|
_ = tmpl.stamp(&a, their_ip, 53);
|
||||||
|
_ = tmpl.stamp(&b, their_ip, 123);
|
||||||
|
try std.testing.expect(!std.mem.eql(u8, a[udp_src_off..][0..2], b[udp_src_off..][0..2]));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,296 @@
|
||||||
|
// ©AngelaMos | 2026
|
||||||
|
// xdp.zig
|
||||||
|
|
||||||
|
const std = @import("std");
|
||||||
|
const linux = std.os.linux;
|
||||||
|
|
||||||
|
pub const AF_XDP: u32 = linux.AF.XDP;
|
||||||
|
pub const SOL_XDP: i32 = linux.SOL.XDP;
|
||||||
|
|
||||||
|
pub const bind_flags = struct {
|
||||||
|
pub const SHARED_UMEM: u16 = 1 << 0;
|
||||||
|
pub const COPY: u16 = 1 << 1;
|
||||||
|
pub const ZEROCOPY: u16 = 1 << 2;
|
||||||
|
pub const USE_NEED_WAKEUP: u16 = 1 << 3;
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const umem_flags = struct {
|
||||||
|
pub const UNALIGNED_CHUNK: u32 = 1 << 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const sockopt = struct {
|
||||||
|
pub const MMAP_OFFSETS: u32 = 1;
|
||||||
|
pub const RX_RING: u32 = 2;
|
||||||
|
pub const TX_RING: u32 = 3;
|
||||||
|
pub const UMEM_REG: u32 = 4;
|
||||||
|
pub const UMEM_FILL_RING: u32 = 5;
|
||||||
|
pub const UMEM_COMPLETION_RING: u32 = 6;
|
||||||
|
pub const STATISTICS: u32 = 7;
|
||||||
|
pub const OPTIONS: u32 = 8;
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const RING_NEED_WAKEUP: u32 = 1 << 0;
|
||||||
|
pub const OPTIONS_ZEROCOPY: u32 = 1 << 0;
|
||||||
|
|
||||||
|
pub const pgoff = struct {
|
||||||
|
pub const RX_RING: u64 = 0;
|
||||||
|
pub const TX_RING: u64 = 0x80000000;
|
||||||
|
pub const FILL_RING: u64 = 0x100000000;
|
||||||
|
pub const COMPLETION_RING: u64 = 0x180000000;
|
||||||
|
};
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(bind_flags.COPY == linux.XDP.COPY);
|
||||||
|
std.debug.assert(bind_flags.ZEROCOPY == linux.XDP.ZEROCOPY);
|
||||||
|
std.debug.assert(bind_flags.USE_NEED_WAKEUP == linux.XDP.USE_NEED_WAKEUP);
|
||||||
|
std.debug.assert(sockopt.MMAP_OFFSETS == linux.XDP.MMAP_OFFSETS);
|
||||||
|
std.debug.assert(sockopt.TX_RING == linux.XDP.TX_RING);
|
||||||
|
std.debug.assert(sockopt.UMEM_REG == linux.XDP.UMEM_REG);
|
||||||
|
std.debug.assert(sockopt.UMEM_FILL_RING == linux.XDP.UMEM_FILL_RING);
|
||||||
|
std.debug.assert(sockopt.UMEM_COMPLETION_RING == linux.XDP.UMEM_COMPLETION_RING);
|
||||||
|
std.debug.assert(sockopt.OPTIONS == linux.XDP.OPTIONS);
|
||||||
|
std.debug.assert(OPTIONS_ZEROCOPY == linux.XDP.OPTIONS_ZEROCOPY);
|
||||||
|
std.debug.assert(pgoff.TX_RING == linux.XDP.PGOFF_TX_RING);
|
||||||
|
std.debug.assert(pgoff.FILL_RING == linux.XDP.UMEM_PGOFF_FILL_RING);
|
||||||
|
std.debug.assert(pgoff.COMPLETION_RING == linux.XDP.UMEM_PGOFF_COMPLETION_RING);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const UmemReg = extern struct {
|
||||||
|
addr: u64,
|
||||||
|
len: u64,
|
||||||
|
chunk_size: u32,
|
||||||
|
headroom: u32,
|
||||||
|
flags: u32,
|
||||||
|
tx_metadata_len: u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const RingOffset = extern struct {
|
||||||
|
producer: u64,
|
||||||
|
consumer: u64,
|
||||||
|
desc: u64,
|
||||||
|
flags: u64,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const MmapOffsets = extern struct {
|
||||||
|
rx: RingOffset,
|
||||||
|
tx: RingOffset,
|
||||||
|
fr: RingOffset,
|
||||||
|
cr: RingOffset,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Desc = extern struct {
|
||||||
|
addr: u64,
|
||||||
|
len: u32,
|
||||||
|
options: u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Options = extern struct {
|
||||||
|
flags: u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
comptime {
|
||||||
|
std.debug.assert(@sizeOf(UmemReg) == 32);
|
||||||
|
std.debug.assert(@sizeOf(RingOffset) == 32);
|
||||||
|
std.debug.assert(@sizeOf(MmapOffsets) == 128);
|
||||||
|
std.debug.assert(@sizeOf(Desc) == 16);
|
||||||
|
std.debug.assert(@sizeOf(Options) == 4);
|
||||||
|
std.debug.assert(@sizeOf(linux.sockaddr.xdp) == 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const Prod = struct {
|
||||||
|
producer: *u32,
|
||||||
|
consumer: *u32,
|
||||||
|
ring: [*]Desc,
|
||||||
|
mask: u32,
|
||||||
|
size: u32,
|
||||||
|
cached_prod: u32 = 0,
|
||||||
|
cached_cons: u32 = 0,
|
||||||
|
|
||||||
|
pub fn reserve(self: *Prod) ?u32 {
|
||||||
|
if (self.cached_prod -% self.cached_cons == self.size) {
|
||||||
|
self.cached_cons = @atomicLoad(u32, self.consumer, .acquire);
|
||||||
|
if (self.cached_prod -% self.cached_cons == self.size) return null;
|
||||||
|
}
|
||||||
|
const slot = self.cached_prod & self.mask;
|
||||||
|
self.cached_prod +%= 1;
|
||||||
|
return slot;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(self: *Prod, slot: u32, desc: Desc) void {
|
||||||
|
self.ring[slot] = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn publish(self: *Prod) void {
|
||||||
|
@atomicStore(u32, self.producer, self.cached_prod, .release);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const Comp = struct {
|
||||||
|
producer: *u32,
|
||||||
|
consumer: *u32,
|
||||||
|
ring: [*]u64,
|
||||||
|
mask: u32,
|
||||||
|
size: u32,
|
||||||
|
cached_prod: u32 = 0,
|
||||||
|
cached_cons: u32 = 0,
|
||||||
|
|
||||||
|
pub fn peek(self: *Comp) u32 {
|
||||||
|
var avail = self.cached_prod -% self.cached_cons;
|
||||||
|
if (avail == 0) {
|
||||||
|
self.cached_prod = @atomicLoad(u32, self.producer, .acquire);
|
||||||
|
avail = self.cached_prod -% self.cached_cons;
|
||||||
|
}
|
||||||
|
return avail;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn addrAt(self: *const Comp, i: u32) u64 {
|
||||||
|
return self.ring[(self.cached_cons +% i) & self.mask];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn release(self: *Comp, n: u32) void {
|
||||||
|
self.cached_cons +%= n;
|
||||||
|
@atomicStore(u32, self.consumer, self.cached_cons, .release);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const FrameStack = struct {
|
||||||
|
free: []u64,
|
||||||
|
top: usize,
|
||||||
|
|
||||||
|
pub fn init(buf: []u64, num_frames: u32, frame_size: u32) FrameStack {
|
||||||
|
var i: u32 = 0;
|
||||||
|
while (i < num_frames) : (i += 1) {
|
||||||
|
buf[i] = @as(u64, i) * frame_size;
|
||||||
|
}
|
||||||
|
return .{ .free = buf, .top = num_frames };
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(self: *FrameStack) ?u64 {
|
||||||
|
if (self.top == 0) return null;
|
||||||
|
self.top -= 1;
|
||||||
|
return self.free[self.top];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(self: *FrameStack, addr: u64) void {
|
||||||
|
std.debug.assert(self.top < self.free.len);
|
||||||
|
self.free[self.top] = addr;
|
||||||
|
self.top += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn available(self: *const FrameStack) usize {
|
||||||
|
return self.top;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
test "FrameStack lays out frame offsets, pops LIFO, and reports empty" {
|
||||||
|
var buf: [4]u64 = undefined;
|
||||||
|
var fs = FrameStack.init(&buf, 4, 2048);
|
||||||
|
try std.testing.expectEqual(@as(usize, 4), fs.available());
|
||||||
|
try std.testing.expectEqual(@as(u64, 6144), fs.pop().?);
|
||||||
|
try std.testing.expectEqual(@as(u64, 4096), fs.pop().?);
|
||||||
|
fs.push(4096);
|
||||||
|
try std.testing.expectEqual(@as(u64, 4096), fs.pop().?);
|
||||||
|
try std.testing.expectEqual(@as(u64, 2048), fs.pop().?);
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), fs.pop().?);
|
||||||
|
try std.testing.expect(fs.pop() == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Prod.reserve backpressures at ring size and recovers as the kernel consumes" {
|
||||||
|
const size: u32 = 4;
|
||||||
|
var producer: u32 = 0;
|
||||||
|
var consumer: u32 = 0;
|
||||||
|
var ring: [size]Desc = undefined;
|
||||||
|
var p = Prod{ .producer = &producer, .consumer = &consumer, .ring = &ring, .mask = size - 1, .size = size };
|
||||||
|
|
||||||
|
var got: u32 = 0;
|
||||||
|
while (p.reserve()) |slot| {
|
||||||
|
p.write(slot, .{ .addr = got, .len = 1, .options = 0 });
|
||||||
|
got += 1;
|
||||||
|
}
|
||||||
|
try std.testing.expectEqual(size, got);
|
||||||
|
try std.testing.expect(p.reserve() == null);
|
||||||
|
|
||||||
|
p.publish();
|
||||||
|
try std.testing.expectEqual(size, @atomicLoad(u32, &producer, .acquire));
|
||||||
|
|
||||||
|
@atomicStore(u32, &consumer, 2, .release);
|
||||||
|
try std.testing.expect(p.reserve() != null);
|
||||||
|
try std.testing.expect(p.reserve() != null);
|
||||||
|
try std.testing.expect(p.reserve() == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "Comp.peek sees kernel completions and release advances the consumer" {
|
||||||
|
const size: u32 = 4;
|
||||||
|
var producer: u32 = 0;
|
||||||
|
var consumer: u32 = 0;
|
||||||
|
var ring: [size]u64 = undefined;
|
||||||
|
var c = Comp{ .producer = &producer, .consumer = &consumer, .ring = &ring, .mask = size - 1, .size = size };
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u32, 0), c.peek());
|
||||||
|
|
||||||
|
ring[0] = 0;
|
||||||
|
ring[1] = 2048;
|
||||||
|
ring[2] = 4096;
|
||||||
|
@atomicStore(u32, &producer, 3, .release);
|
||||||
|
|
||||||
|
try std.testing.expectEqual(@as(u32, 3), c.peek());
|
||||||
|
try std.testing.expectEqual(@as(u64, 0), c.addrAt(0));
|
||||||
|
try std.testing.expectEqual(@as(u64, 2048), c.addrAt(1));
|
||||||
|
try std.testing.expectEqual(@as(u64, 4096), c.addrAt(2));
|
||||||
|
c.release(3);
|
||||||
|
try std.testing.expectEqual(@as(u32, 3), @atomicLoad(u32, &consumer, .acquire));
|
||||||
|
try std.testing.expectEqual(@as(u32, 0), c.peek());
|
||||||
|
}
|
||||||
|
|
||||||
|
test "TX and completion rings recycle a bounded UMEM frame pool across kicks" {
|
||||||
|
const size: u32 = 8;
|
||||||
|
const num_frames: u32 = 8;
|
||||||
|
const frame_size: u32 = 2048;
|
||||||
|
|
||||||
|
var tx_prod: u32 = 0;
|
||||||
|
var tx_cons: u32 = 0;
|
||||||
|
var tx_ring: [size]Desc = undefined;
|
||||||
|
var cq_prod: u32 = 0;
|
||||||
|
var cq_cons: u32 = 0;
|
||||||
|
var cq_ring: [size]u64 = undefined;
|
||||||
|
|
||||||
|
var tx = Prod{ .producer = &tx_prod, .consumer = &tx_cons, .ring = &tx_ring, .mask = size - 1, .size = size };
|
||||||
|
var cq = Comp{ .producer = &cq_prod, .consumer = &cq_cons, .ring = &cq_ring, .mask = size - 1, .size = size };
|
||||||
|
|
||||||
|
var fb: [num_frames]u64 = undefined;
|
||||||
|
var frames = FrameStack.init(&fb, num_frames, frame_size);
|
||||||
|
|
||||||
|
var total_sent: u32 = 0;
|
||||||
|
var round: u32 = 0;
|
||||||
|
while (round < 4) : (round += 1) {
|
||||||
|
var i: u32 = 0;
|
||||||
|
while (i < size) : (i += 1) {
|
||||||
|
const off = frames.pop() orelse break;
|
||||||
|
const slot = tx.reserve() orelse {
|
||||||
|
frames.push(off);
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
tx.write(slot, .{ .addr = off, .len = 54, .options = 0 });
|
||||||
|
total_sent += 1;
|
||||||
|
}
|
||||||
|
tx.publish();
|
||||||
|
|
||||||
|
const published = @atomicLoad(u32, &tx_prod, .acquire);
|
||||||
|
var kc = @atomicLoad(u32, &tx_cons, .monotonic);
|
||||||
|
var kp = @atomicLoad(u32, &cq_prod, .monotonic);
|
||||||
|
while (kc != published) : (kc +%= 1) {
|
||||||
|
cq_ring[kc & (size - 1)] = tx_ring[kc & (size - 1)].addr;
|
||||||
|
kp +%= 1;
|
||||||
|
}
|
||||||
|
@atomicStore(u32, &tx_cons, kc, .release);
|
||||||
|
@atomicStore(u32, &cq_prod, kp, .release);
|
||||||
|
|
||||||
|
const n = cq.peek();
|
||||||
|
var k: u32 = 0;
|
||||||
|
while (k < n) : (k += 1) frames.push(cq.addrAt(k));
|
||||||
|
if (n > 0) cq.release(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
try std.testing.expect(total_sent > num_frames);
|
||||||
|
try std.testing.expectEqual(@as(usize, num_frames), frames.available());
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# ©AngelaMos | 2026
|
||||||
|
# uninstall.sh
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
BINARY="zingela"
|
||||||
|
INSTALL_DIR="${ZINGELA_INSTALL_DIR:-$HOME/.local/bin}"
|
||||||
|
CACHE_SRC="${XDG_CACHE_HOME:-$HOME/.cache}/${BINARY}-src"
|
||||||
|
CACHE_ZIG="${XDG_CACHE_HOME:-$HOME/.cache}/${BINARY}-zig"
|
||||||
|
|
||||||
|
if [ -t 2 ] && [ -z "${NO_COLOR:-}" ]; then
|
||||||
|
BOLD=$'\033[1m'; DIM=$'\033[2m'; GREEN=$'\033[32m'
|
||||||
|
YELLOW=$'\033[33m'; VIOLET=$'\033[38;2;139;92;246m'; RESET=$'\033[0m'
|
||||||
|
else
|
||||||
|
BOLD=""; DIM=""; GREEN=""; YELLOW=""; VIOLET=""; RESET=""
|
||||||
|
fi
|
||||||
|
info() { printf '%s\n' " ${VIOLET}+${RESET} $*" >&2; }
|
||||||
|
ok() { printf '%s\n' " ${GREEN}+${RESET} $*" >&2; }
|
||||||
|
warn() { printf '%s\n' " ${YELLOW}!${RESET} $*" >&2; }
|
||||||
|
have() { command -v "$1" >/dev/null 2>&1; }
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
cat >&2 <<USAGE
|
||||||
|
uninstall.sh - remove ${BINARY}
|
||||||
|
|
||||||
|
./uninstall.sh [--prefix DIR]
|
||||||
|
USAGE
|
||||||
|
}
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
--prefix) INSTALL_DIR="$2"; shift 2 ;;
|
||||||
|
--prefix=*) INSTALL_DIR="${1#*=}"; shift ;;
|
||||||
|
-h|--help) usage; exit 0 ;;
|
||||||
|
*) warn "unknown option: $1"; shift ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
main() {
|
||||||
|
printf '\n%s\n\n' "${BOLD}${VIOLET}--- removing ${BINARY} ---${RESET}" >&2
|
||||||
|
|
||||||
|
local dest="$INSTALL_DIR/$BINARY"
|
||||||
|
if [ -e "$dest" ]; then rm -f "$dest"; ok "removed $dest"; else info "no binary at $dest"; fi
|
||||||
|
|
||||||
|
for c in "$CACHE_SRC" "$CACHE_ZIG"; do
|
||||||
|
if [ -d "$c" ]; then rm -rf "$c"; ok "removed cache $c"; fi
|
||||||
|
done
|
||||||
|
|
||||||
|
warn "if you want to drop it from PATH, delete this line from your shell rc:"
|
||||||
|
printf '%s\n' " ${DIM}export PATH=\"${INSTALL_DIR}:\$PATH\"${RESET}" >&2
|
||||||
|
|
||||||
|
printf '\n%s\n\n' " ${GREEN}${BOLD}${BINARY} removed.${RESET}" >&2
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@" </dev/null
|
||||||
|
|
@ -25,7 +25,7 @@
|
||||||
<h2 align="center"><strong>View Complete Projects:</strong></h2>
|
<h2 align="center"><strong>View Complete Projects:</strong></h2>
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<a href="https://github.com/CarterPerez-dev/Cybersecurity-Projects/tree/main/PROJECTS">
|
<a href="https://github.com/CarterPerez-dev/Cybersecurity-Projects/tree/main/PROJECTS">
|
||||||
<img src="https://img.shields.io/badge/Full_Source_Code-34/70-blue?style=for-the-badge&logo=github" alt="Projects"/>
|
<img src="https://img.shields.io/badge/Full_Source_Code-36/70-blue?style=for-the-badge&logo=github" alt="Projects"/>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -123,9 +123,9 @@ Tools, courses, certifications, communities, and frameworks for cybersecurity pr
|
||||||
| **[Credential Enumeration](./PROJECTS/intermediate/credential-enumeration)**<br>Post-exploitation credential collection |    | Credential extraction • Browser forensics • Red team tooling • Nim language<br>[Source Code](./PROJECTS/intermediate/credential-enumeration) \| [Docs](./PROJECTS/intermediate/credential-enumeration/learn) |
|
| **[Credential Enumeration](./PROJECTS/intermediate/credential-enumeration)**<br>Post-exploitation credential collection |    | Credential extraction • Browser forensics • Red team tooling • Nim language<br>[Source Code](./PROJECTS/intermediate/credential-enumeration) \| [Docs](./PROJECTS/intermediate/credential-enumeration/learn) |
|
||||||
| **[Binary Analysis Tool](./PROJECTS/intermediate/binary-analysis-tool)**<br>Disassemble and analyze executables |    | Binary analysis • String extraction • Malware detection<br>[Source Code](./PROJECTS/intermediate/binary-analysis-tool) \| [Docs](./PROJECTS/intermediate/binary-analysis-tool/learn)<br>[](https://elitenetwork4life.com/) |
|
| **[Binary Analysis Tool](./PROJECTS/intermediate/binary-analysis-tool)**<br>Disassemble and analyze executables |    | Binary analysis • String extraction • Malware detection<br>[Source Code](./PROJECTS/intermediate/binary-analysis-tool) \| [Docs](./PROJECTS/intermediate/binary-analysis-tool/learn)<br>[](https://elitenetwork4life.com/) |
|
||||||
| **[Chaos Engineering Security Tool](./SYNOPSES/intermediate/Chaos.Engineering.Security.Tool.md)**<br>Inject security failures to test resilience |    | Chaos engineering • Security resilience • Credential spraying • Auth testing<br>[Learn More](./SYNOPSES/intermediate/Chaos.Engineering.Security.Tool.md) |
|
| **[Chaos Engineering Security Tool](./SYNOPSES/intermediate/Chaos.Engineering.Security.Tool.md)**<br>Inject security failures to test resilience |    | Chaos engineering • Security resilience • Credential spraying • Auth testing<br>[Learn More](./SYNOPSES/intermediate/Chaos.Engineering.Security.Tool.md) |
|
||||||
| **[Credential Rotation Enforcer](./SYNOPSES/intermediate/Credential.Rotation.Enforcer.md)**<br>Track and enforce credential rotation policies |    | Credential hygiene • Secret rotation • Compliance dashboards • API integration<br>[Source Code](./PROJECTS/intermediate/credential-rotation-enforcer) \| [Docs](./PROJECTS/intermediate/credential-rotation-enforcer/learn) |
|
| **[Credential Rotation Enforcer](./SYNOPSES/intermediate/Credential.Rotation.Enforcer.md)**<br>Track and enforce credential rotation policies |    | Credential hygiene • Secret rotation • Compliance dashboards • API integration<br>[Source Code](./PROJECTS/intermediate/credential-rotation-enforcer) \| [Docs](./PROJECTS/intermediate/credential-rotation-enforcer/learn) |
|
||||||
| **[Race Condition Exploiter](./SYNOPSES/intermediate/Race.Condition.Exploiter.md)**<br>TOCTOU race condition attack & defense lab |     | TOCTOU attacks • Double-spend bugs • Concurrent exploitation • Race visualization<br>[Learn More](./SYNOPSES/intermediate/Race.Condition.Exploiter.md) |
|
| **[Race Condition Exploiter](./SYNOPSES/intermediate/Race.Condition.Exploiter.md)**<br>TOCTOU race condition attack & defense lab |     | TOCTOU attacks • Double-spend bugs • Concurrent exploitation • Race visualization<br>[Learn More](./SYNOPSES/intermediate/Race.Condition.Exploiter.md) |
|
||||||
| **[Self-Hosted Shodan Clone](./SYNOPSES/intermediate/Self.Hosted.Shodan.Clone.md)**<br>Internet-connected device search engine |     | Service fingerprinting • Network scanning • OSINT • Search engine design<br>[Learn More](./SYNOPSES/intermediate/Self.Hosted.Shodan.Clone.md) |
|
| **[Zingela Stateless Scanner](./PROJECTS/advanced/zig-stateless-scanner)**<br>Line-rate stateless mass TCP/UDP port scanner |    | Stateless SYN scanning • SipHash cookies • Cyclic-group permutation • AF_PACKET / AF_XDP<br>[Source Code](./PROJECTS/advanced/zig-stateless-scanner) \| [Docs](./PROJECTS/advanced/zig-stateless-scanner/learn) |
|
||||||
| **[JA3/JA4 TLS Fingerprinting Tool](./SYNOPSES/intermediate/JA3.JA4.TLS.Fingerprinting.Tool.md)**<br>Fingerprint TLS clients by handshake |    | TLS handshake analysis • JA3/JA4 hashing • Bot detection • Malware C2 identification<br>[Source Code](./PROJECTS/intermediate/ja3-ja4-tls-fingerprinting) \| [Docs](./PROJECTS/intermediate/ja3-ja4-tls-fingerprinting/learn)<br>[](https://mkultraalumni.com/) |
|
| **[JA3/JA4 TLS Fingerprinting Tool](./SYNOPSES/intermediate/JA3.JA4.TLS.Fingerprinting.Tool.md)**<br>Fingerprint TLS clients by handshake |    | TLS handshake analysis • JA3/JA4 hashing • Bot detection • Malware C2 identification<br>[Source Code](./PROJECTS/intermediate/ja3-ja4-tls-fingerprinting) \| [Docs](./PROJECTS/intermediate/ja3-ja4-tls-fingerprinting/learn)<br>[](https://mkultraalumni.com/) |
|
||||||
| **[Mobile App Security Analyzer](./SYNOPSES/intermediate/Mobile.App.Security.Analyzer.md)**<br>Decompile and analyze mobile apps |    | APK/IPA analysis • Reverse engineering • OWASP Mobile<br>[Learn More](./SYNOPSES/intermediate/Mobile.App.Security.Analyzer.md) |
|
| **[Mobile App Security Analyzer](./SYNOPSES/intermediate/Mobile.App.Security.Analyzer.md)**<br>Decompile and analyze mobile apps |    | APK/IPA analysis • Reverse engineering • OWASP Mobile<br>[Learn More](./SYNOPSES/intermediate/Mobile.App.Security.Analyzer.md) |
|
||||||
| **[DLP Scanner](./PROJECTS/intermediate/dlp-scanner)**<br>Data Loss Prevention for files, DBs, and traffic |    | PII detection • GDPR/HIPAA compliance • Pattern matching • Data classification<br>[Source Code](./PROJECTS/intermediate/dlp-scanner) \| [Docs](./PROJECTS/intermediate/dlp-scanner/learn) |
|
| **[DLP Scanner](./PROJECTS/intermediate/dlp-scanner)**<br>Data Loss Prevention for files, DBs, and traffic |    | PII detection • GDPR/HIPAA compliance • Pattern matching • Data classification<br>[Source Code](./PROJECTS/intermediate/dlp-scanner) \| [Docs](./PROJECTS/intermediate/dlp-scanner/learn) |
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue