6 ms·
> (2) everything (ish) is a file I'm not all that knowledgable about Unix history, but one thing that has always puzzled me was that for whatever reason networ
by ryandrake 6y ago
> (2) everything (ish) is a file
I'm not all that knowledgable about Unix history, but one thing that has always puzzled me was that for whatever reason network connections (generally) aren't files. While I can do:
cat < /dev/ttyS0
to read from a serial device, I've always wondered why I can't do something like:
bind /tmp/mysocket 12.34.56.78 80
cat < /tmp/mysocket
It is weird that so many things in Unix are somehow twisted into files (/dev/fb0??), but network connections, to my knowledge, never managed to go that way. I know we have netcat but it's not the same.
- moonchild 6y agoIn the c api, you can read/write to sockets. Wouldn't be difficult to implement something like your example with a named pipe. Additionally, there is /dev/tcp in bash.
- spijdar 6y agoThe original authors of "UNIX" considered their creation to be fatally flawed, and created a successor OS called "Plan 9", partially to address this specific deficit, among others like the perceived tacked-on nature of GUIs on *nix. That's a whole rabbit hole to go down, but Plan 9 networking is much more like your hypothetical example [0]. Additionally, things like the framebuffer and devices are also exposed as pure files, whereas on Linux and most unix-like systems such devices are just endpoints for making ioctl() calls. [0] http://doc.cat-v.org/plan_9/4th_edition/papers/net/ http://doc.cat-v.org/plan_9/4th_edition/papers/net/
- DSMan195276 6y agoI think you're just seeing the abstraction fall apart a bit - for weirder devices, I would say they are "accessible" via a file, but you likely can't interact with it without special tools/syscalls/ioctls. For example, cat'ing your serial connection won't always work, occasionally you'll need to configure the serial device or tty settings first and things can get pretty messy. That's why programs like `minicom` exist. For networking, I don't think there's a particular reason it doesn't exist, but it's worth noting sockets are a little special and require a bit extra care (Which is why they have special syscalls like `shutdown()` and `send()` and `recv()`). If you did pass a TCP socket to `cat` or other programs (Which you can do! just a bit of fork-exec magic), you'd discover it doesn't always work quite right. And while I don't know the history on why such a feature doesn't exist, with the fact that tools like socat or curl do the job pretty well I don't think it's seen as that necessary.
- XorNot 6y agoTheoretically it would work if the devices were exposed as directories, not files (which is basically what /proc actually does for most things). /dev/ttyS0 should really be /dev/ttyS0/{data, ctr, baud, parity, stopbits, startbits} (probably a bunch I'm forgetting).
- AnIdiotOnTheNet 6y agoCongratulations, you've re-invented Plan9!
- jandrese 6y agoBerkeley sockets were kind of bolted on as an afterthought. If the AT&T guys developed them they would probably look a lot more like that.
- JdeBP 6y agoThere's no need for a subjunctive. Go and look at AT&T STREAMS and see what actually did result.
- yjftsjthsd-h 6y agoSounds like you want netcat?
- AdrianoKF 6y agoOr potentially even socat (http://www.dest-unreach.org/socat/ http://www.dest-unreach.org/socat/), which could be used (among many other things) to model the use case of GP, forwarding a socket to a pipe.
- philsnow 6y agoelsewhere in this thread somebody mentions socat, but you can do it entirely within bash. from https://www.linuxjournal.com/content/more-using-bashs-built-devtcp-file-tcpip https://www.linuxjournal.com/content/more-using-bashs-built-... : exec 3<>/dev/tcp/www.google.com/80 echo -e "GET / HTTP/1.1\r\nhost: http://www.google.com\r\nConnection: close\r\n\r\n" >&3 cat <&3 https://news.ycombinator.com/item?id=23422423 https://news.ycombinator.com/item?id=23422423 has a good point that "everything is a file" is maybe less useful than "everything is a file descriptor". the shell is a tool for setting up pipelines of processes and linking their file descriptors together.
- JdeBP 6y agoDaniel J. Bernstein's little known http@ tool is just a shell script wrapped around tcpclient, and is fairly similar, except that it does not use the Bashisms of course: printf 'GET /%s HTTP/1.0\nHost: %s:%s\n\n' "${2-}" "${1-0}" "${3-80}" | /usr/local/bin/tcpclient -RHl0 -- "${1-0}" "${3-80}" sh -c ' /usr/local/bin/addcr >&7 exec /usr/local/bin/delcr <&6 ' | awk '/^$/ { body=1; next } { if (body) print }'