Plan 9 from Bell Labs’s /usr/web/sources/contrib/rog/sh-examples/net.sh

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


# define some functions to access network devices.
# ${cs addr}
#	translate tcp!somewhere.net!23 into /net/tcp/clone 123.45.67.8!23
# dial address {command}
#	dial address and run command with stdin connected
#	to the remote end, and $net set to the net directory.
# announce address {command}
#	announce an address; run command as in dial above.
#	(actually, it's probably not worth opening the data file in
#	this case, as it doesn't go anywhere)
# listen address {command}
#	listen on a previously announced port; assumes $net
#	is set to previously announce directory; runs command
# 	when an incoming connection arrives, as for dial, above.

# server, client: sample uses of the above.

#{
#	id := "{read}
#	net := /net/udp/$id
#	(if {echo -n connect 255.255.255.255!1442 >[1=0]}
#		{echo -n 'tx data' > $net/data}
#		{echo connection failed}
#	)
#} <> /net/udp/clone
#
#{
#	id := "{read}
#	net := /net/udp/$id
#	echo -n announce 1442 >[1=0]
#	echo -n headers >[1=0]
#	while {} {s := "{read | bytes 6 end}; echo got rq $s} < $net/data
#} <> /net/udp/clone
#
load string std file2chan

subfn cs {
	args := $*
	or {
		{
			(if {echo -n ${hd $args} >[1=0]}
				{result=`{read 8192 0}}
				{echo 'addr translate failed:' $status; status failed}
			)
		} <> /net/cs
	} {raise 'cs failed'}
}

fn dial {
	args := $*
	if {or {! ~ $#args 2} {! ~ ${tl $args} '{*'}} {echo 'usage: dial address {cmd}' >[1=2]; raise failed}
	cs := ${cs ${hd $args}}
	{
		id := "{read 20 0}
		or {echo -n connect ${tl $cs} >[1=0]} {echo 'dial failed:' $status; raise failed}
		net := ${hd ${splitr ${hd $cs} /}}^$id
		${tl $args} <> $net/data
	} <> ${hd $cs}
}

fn announce {
	args := $*
	if {or {! ~ $#args 2} {! ~ ${tl $args} '{*'}} {echo 'usage: announce address {cmd}' >[1=2]; raise failed}
	cs := ${cs ${hd $args}}
	{
		id := "{read 20 0}
		or {echo -n announce ${tl $cs} >[1=0]} {echo 'announce failed:' $status; raise failed}
		net := ${hd ${splitr ${hd $cs} /}}^$id
		${tl $args} <> $net/data
	} <> ${hd $cs}
}

fn listen {
	args := $*
	if {or {! ~ $#args 1} {! ~ $args '{*'}} {echo 'usage: listen {cmd}' >[1=2]; raise failed}
	if {! ~ $#net 1} {echo 'listen: no $net set' >[1=2]; raise 'bad context'}
	{
		id := "{read 20 0}
		net := ${hd ${splitr ${hd $net} /}}^$id
		$args <> $net/data
	} <> $net/listen
}

#fn server {
#	file2chan /chan/xxx {} {echo got write request ${quote ${rget data}}}
#	announce 'tcp!*!62454' {
#		echo done announce
#		while {} {
#			listen {
#				echo got incoming connection
#				export /chan &
#			}
#		}
#	}
#}
#
#fn client {
#	dial 'tcp!presto!62454' {
#		echo 'made connection; dir:' $net
#		imount $net/data /n/remote
#	}
#	echo look in /n/remote
#}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.