3

In bash, we can use file descriptors in order to (try to) open socket descriptors, as follow:

exec 3<>/dev/prot/host/port

where:

  • prot can be tcp or udp;
  • host can be a valid hostname or Internet address;
  • port can be an integer port number or service name.

This is just an example and any redirection can be used
(E.G. printf "%s" "${payload}" >"/dev/udp/${host}/${port}").
This is true at least starting from bash v4+.

Someone else already asked to accomplish the very same thing without a bash built-in;
I already found and tried other solutions, including (but not limited to):

  • nc/netcat/nmap/tcping (or similar), in order to "scan" (local or remote) ports (some of those will not be able to receive payloads);
  • netstat/ss (or similar), in order to know LISTEN-state (local) ports (and cannot send nor receive any payload);

Among the others, nc could be considered the most portable solution for this task even though (at least in my understandings) it should not be in coreutils; while solutions as ss cannot be considered valid for this task because they cannot "scan" another host and cannot send nor receive payloads;
Anyway, it happened to me to find some environment where nc was not installed.

I can't assume other interpreters are installed (E.G. python, perl, etc.);
I can't assume compilers are installed.

Finally, as you already guessed, the question:
There is some posix-way, using anything but a shell, to accomplish a full connection?

In other words, there is a portable way to make use of connections?

ingroxd
  • 995
  • 1
  • 12
  • 28
  • As far as I know, network sockets are *entirely* undefined by POSIX. – chepner Feb 25 '19 at 13:50
  • @chepner As stated [here](http://www.openss7.org/papers/strsock/sockimp.pdf), among various standards, there is a standard defined by POSIX on Network Sockets. Plus, in the POSIX Programmer's Manual is described `connect()` as system call (you can use `man connect` to read it) which clearly makes use of Network Sockets. Plus#2, in the POSIX Programmer's Manual is described `socket()` which return a file descriptor (you can use `man socket` to read it). The two of them are conforming to POSIX.1-2001 and POSIX.1-2008. – ingroxd Feb 25 '19 at 14:54
  • Network sockets are just one kind of socket; POSIX provides for general socket support, but doesn't mandate support for internet sockets (which are the kind you are asking about). See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_socket.h.html. – chepner Feb 25 '19 at 15:00
  • @chepner Please, help me understand. I don't need arguing on who is right or not... The lib you linked same the same I provided previously. In [Wikipedia](https://en.wikipedia.org/wiki/Network_socket#Definition), Network Socket is actually called `Internet Socket`, divided in 3 types. Furthermore, I am not searching a way to write my own code, because as I wrote in my question, I can't assume compilers are available. If the problem is, as you said, POSIX doesn't provide a description of Network Socket, there aren't issues because it does. I'm searching a portable way of doing it in shell. – ingroxd Feb 25 '19 at 17:44
  • I'm not sure I can be clearer. "Portable" means that there is some established standard that different implementations agree to follow. No such standard exists for network sockets, so there is no "portable" way to do it. It depends entirely on what your OS or on what additional libraries you install. – chepner Feb 25 '19 at 17:49
  • @ingroxd Did you ever manage to find an answer to this question? – Tenders McChiken Jul 16 '20 at 16:26
  • 1
    @TendersMcChiken No mate, at last I used nc, sticking with options that are valid either for traditional nc, openbsd nc and busybox nc, for portability's sake (options -l, -p, -w and -i only). Usually you do not need to check where nc lays, but I advise you to do so and set a proper alias/var to call it. – ingroxd Jul 21 '20 at 11:23

0 Answers0