Skip to content

Commit

Permalink
Support listening on multiple addresses and connecting at the same time
Browse files Browse the repository at this point in the history
This change has numerous additional features but comes with breaking changes to arguments. The new usage offers strictly greater capabilities than before.

One major new capability is that you can now specify `-l` or `-L` and *also* specify targets to connect to. netcrab will start listening sockets and also start outbound connections. The resulting pool of connections can be brokered or channeled like before.

The other major new capability is that you can specify `-l` or `-L` multiple times to listen on more than one local address and port at a time.

Another secondary capability is that `-s`, `-l`, and `-L` support any hostname format that can be handled by a DNS lookup, including both numeric addresses and non-numeric hostnames.

The `-p` argument to specify the local port number has been removed entirely. Now the local port is specified in two ways:
- for outbound connections, it is given by `-s ADDR:PORT`
- for listening connections, it is given by `-l ADDR:PORT` or `-L ADDR:PORT`

The `ADDR:PORT` syntax also supports `*:PORT` and `*` as options to bind to the wildcard address.

The code no longer really treats "listening" and "outbound" as different scenarios. An outbound connection scenario is the same as listening, except no local addresses were chosen to listen on. Likewise, a listening scenario is just one where no outbound connection targets were specified. There are just `do_tcp` and `do_udp` now.

Outbound TCP connections now print out the local port they're attempting the connection from.
  • Loading branch information
knutaf committed Sep 21, 2023
1 parent 37d7825 commit 071b424
Show file tree
Hide file tree
Showing 4 changed files with 323 additions and 254 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "netcrab"
description = "A multi-purpose TCP/UDP listener and connector"
keywords = ["tcp", "udp", "networking", "sockets"]
categories = ["network-programming", "command-line-utilities"]
version = "0.6.1"
version = "0.7.0"
authors = ["knutaf"]
edition = "2021"
repository = "https://github.com/knutaf/netcrab"
Expand Down
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## Overview

Netcrab is a command-line networking tool that can do a lot of things. It is an homage to [netcat](https://nmap.org/ncat/) and can do several of the things it can. A brief overview of the things it can do:
Netcrab is a command-line networking tool that can do a lot of things. It is an homage to [netcat](https://nmap.org/ncat/) and can do many of the things it can. A brief overview of the things it can do:

- send TCP traffic in client or server mode
- send and receive UDP datagrams
Expand All @@ -23,31 +23,49 @@ The IPv6 format for the hostname should use surrounding square brackets, for exa

## TCP server

`netcrab -L -p PORT`
`netcrab -L ADDR:PORT`

You can listen as a TCP server on an arbitrary port. Using `-l` exits the program after the first incoming connection disconnects. Use `-L` to "listen harder": continue listening after the disconnection.
`netcrab -l ADDR:PORT`

The TCP server by default only allows a single incoming connection to be active at a time, but the `-m max_clients` flag allows more than one to connect.
You can listen as a TCP server on one or more arbitrary ports. Using `-l` exits the program after the first incoming connection disconnects. Use `-L` to "listen harder": continue listening after the disconnection.

The `ADDR:PORT` syntax supports some special variants:
- *HOST*:*PORT* - standard format, anything that can be parsed as a local address, including DNS lookup. E.g. `localhost:5000`
- :*PORT* - automatically enumerates all local addresses. E.g. `:5000`
- \*:*PORT* - uses the wildcard IPv4 and IPv6 addresses (0.0.0.0 and [::]) with the specified port. E.g. `*:5000`
- \* - same as above but implicitly use port 0

The TCP server by default only allows a single incoming connection to be active at a time, but the `-m max_inbound_clients` flag allows more than one to connect.

As with client mode, stdin is sent to all connected sockets, and incoming data from all sockets is sent to stdout. This can be changed using the input and output mode arguments below.

The `-z` argument causes the socket to immediately disconnect without allowing sending any data. Useful for just testing connectivity.

## UDP endpoint

`netcrab -u -L -p PORT`
`netcrab -u -L ADDR:PORT`

`netcrab -u HOST:PORT`

UDP is weird in that you don't really "connect" with it. You bind to a local port and then send/receive datagrams to/from remote peers. Listening mode and connecting mode for UDP work exactly the same, except that in connecting mode, the first peer to send stdin to is known at the start, and in listening mode, stdin traffic can't be sent anywhere until the listener receives at least one datagram from a peer.

For `-L` this supports the same `ADDR:PORT` syntax as above for TCP.

Datagram size defaults to 1 byte but can be controlled by the `--sb` argument.

## Listening on multiple local sockets

`netcrab -L ADDR1:PORT1 -L ADDR2:PORT2`

`netcrab -u -L ADDR1:PORT1 -L ADDR2:PORT2`

Netcrab supports listening on multiple local addresses and ports at the same time. It will accept connections that arrive on any of them. This supports TCP and UDP.

## UDP Multicast support

`netcrab -u -mc HOST:PORT`
`netcrab -u --mc HOST:PORT`

Netcrab supports joining UDP sockets to multicast groups by adding the `-mc` argument. It also gives controls for the TTL for multicast packets (`-ttl`) and whether to receive multicast packets looped back since the program is joined to the group (`-mc_no_loop`).
Netcrab supports joining UDP sockets to multicast groups by adding the `--mc` argument. It also gives controls for the TTL for multicast packets (`--ttl`) and whether to receive multicast packets looped back since the program is joined to the group (`--mc_no_loop`).

## UDP Broadcast support

Expand Down Expand Up @@ -76,7 +94,7 @@ By default, output goes to stdout, but it's often useful to change it to `-o non

## IO redirection

The default mode of netcrab is to use stdin and stdout, so you can redirect input from a file and send output to a file (or piped between programs).
The default mode of Netcrab is to use stdin and stdout, so you can redirect input from a file and send output to a file (or piped between programs).

`netcrab HOST:PORT < file`

Expand All @@ -90,18 +108,24 @@ The default mode of netcrab is to use stdin and stdout, so you can redirect inpu

You can restrict to using only IPv6 or IPv4 address families. This makes more of a difference when connecting to hostnames that go through DNS resolution or when listening without specifying an explicit source address.

## Controlling source address
## Controlling source address for outbound connections

`netcrab -s SOURCE_ADDR`
`netcrab -s ADDR:PORT`

By default, netcrab binds to the unspecified IPv4 and IPv6 addresses. You can pass `-s` to explicitly bind to an address instead.
When making an outbound TCP connection or sending UDP datagrams, by default Netcrab binds to the wildcard IPv4 and IPv6 addresses (0.0.0.0:0 and [::]:0). You can pass `-s` to explicitly bind to an address instead. This supports all the `ADDR:PORT` variants described in the "TCP Server" section.

## Connecting to multiple outbound targets

`netcrab HOST1:PORT1 HOST2:PORT2`

Netcrab allows connecting to more than one remote peer at the same time. Similarly to listening for multiple concurrent connections, traffic from the local machine will be sent to all connected peers, inbound or outbound.

## Listening and connecting at the same time

`netcrab -L ADDR:PORT HOST:PORT`

Netcrab supports both listening for inbound connections and making outbound connections at the same time. This could be useful for proxying traffic from one local address to another, since you can independently specify both the address to listen on and the source address to use for the outbound connection.

## Multiple connections to an outbound target

`netcrab HOST:PORTxNUM`
Expand All @@ -116,7 +140,7 @@ This can also be combined with multiple targets. Here we connect to localhost on

`netcrab -R`

In outbound connection mode, you can ask netcrab to re-establish a dropped connection. `-r` re-establishes on graceful connection close. `-R` re-establishes on ungraceful error. Both can be specified at the same time.
In outbound connection mode, you can ask Netcrab to re-establish a dropped connection. `-r` re-establishes on graceful connection close. `-R` re-establishes on ungraceful error. Both can be specified at the same time, either `-r -R` or `-rR`.

## Channels mode

Expand Down Expand Up @@ -146,3 +170,7 @@ When in channels mode, max clients is automatically bumped to 10 under the assum
Broker mode is similar to channels mode, but simpler: all traffic from all network sources is forwarded back to all other sockets. You could use it to set up a chat room or something.

As in channels mode, when in broker mode, max clients is automatically bumped to 10 but can be overridden with `-m`.

## Endless possibilities

Pretty much all of the capabilities described above can be combined and used at the same time. For example, listening on multiple addresses and connecting to multiple targets in the same session while brokering all the connections can be done.
Loading

0 comments on commit 071b424

Please sign in to comment.