Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Trivial P2P in newLISP

; a trivial P2P file sharing program written in newLISP for demo purpose
;
; based on ideas from http://www.freedom-to-tinker.com/tinyp2p.html
; and http://ansuz.sooke.bc.ca/software/molester/ and
; http://ansuz.sooke.bc.ca/software/molester/2005010301.php
;
; command reference
; i/ advertise presence of your node to the peer
; g<filename>/ requests a file
; f<message> forward to peers
; h/ gets list of all peers
;
; used internally
; e<filename>/ expect a file
; x sent after receiving a file to make sure
;
; the program below is a toy, not a serious p2p program.
;
; differences from original mole-ster:
; use of 'x' -- to allow data receipt on receiving side when file is sent
; data is read in 8k chunks at a time. this is to avoid having to read
; the entire file into a buffer before writing. it allows larger files to be
; transferred.
;
; more more information refer to the original mole-ster web sites.
;

(context 'P2P)

(constant 'SIGINT 2)
(define (interrupted)
  (println "interruted by user!")
  (exit))

(signal SIGINT interrupted)

(set 'my-address "")
(set 'my-password "")
(set 'peers '())

(define (get-addr addr-and-port)  (regex "(.*):(.*)" addr-and-port)  $1)
(define (get-port addr-and-port)  (regex "(.*):(.*)" addr-and-port)  (integer $2))

(define (op-send dest-addr source-addr filename data)
  (if (set 'socket (net-connect (get-addr dest-addr) (get-port dest-addr)))
      (begin
			(net-send socket (format "%s %s %s/" my-password source-addr filename))
			(net-send socket data )
			(if (!= data "")
				(net-receive socket 'buf 1))
			(close socket))))

(define (P2P:P2P my-password peer-address my-address commands )
  (set 'peers (append peers (list peer-address)))
  (dolist (cmd commands)  (op-send peer-address my-address cmd ""))
  (set 'socket (net-listen  (get-port my-address)))
  (while true 
	 (while  (and (not (net-error)) (not (net-select socket "read" 1000)))
		 (if (net-error) (print (net-error))))
	 (set 'peer-socket (net-accept socket)) (net-receive peer-socket 'buf 1024 "/") 
	 (regex "^([a-zA-Z0-9]*) ([0-9:.]*) ([e-i])([^/]*)(/)" buf) 
	 (set 'peer-password $1) 
	 (set 'peer-address $2) 
	 (set 'peer-command $3) 
	 (set 'requested-filename $4) 
	 (set 'data $6) 
	 (if (= peer-password my-password) 
	     (case peer-command
	       ("e" (begin
	       			(set 'finished false)
	       			(while (not finished)
	       		     	(while (and (not (net-error)) (not (net-select peer-socket "read" 1000)))
	       		     		(if (net-error) (print (net-error))))
			    		(if (!= nil (net-receive peer-socket 'input-data 8192))
			    			(begin
								(append-file  requested-filename input-data)
							(set 'finished true)))
					(net-send socket "x")))
	       ("f"  (dolist (peer peers)   (op-send peer my-address requested-filename data)))
	       ("g" (op-send peer-address my-address (append "e" requested-filename) 
	       			(read-file requested-filename)))
	       ("h" (dolist (peer peers)  (op-send peer-address peer "i" "")))
	       ("i" (append peers peer-address))))
	 (close peer-socket)))

(context 'MAIN)


(P2P:P2P (main-args 2) (main-args 3) (main-args 4)  (slice (main-args) 5 -1))


uP2P — micro P2P file sharing application

#!/bin/sh
# uP2P.sh 0.0.1, 436 characters (excluding comments)
[ $3 ]&&export W=$1 H="$2 $3" K=`mktemp`;Z=/dev/null;e(){ echo "$*";};n(){
nc $* 2>$Z;};x(){ nc -lp ${H#* } -e $1 &>$Z <$Z&};f(){ cat $K|while read h;do
e $W $1 "$2"|n $h;done };case $# in 4)e $W s "$4"|n $H|while read h p f; do
e $W g "$f"|n $h $p>"$f";done;;5)e $H>$K;e $W d $H|n $4 $5>>$K;x $0;;0)x $0
read w c r;[ $W = $w ]&&case $c in s)f l "$r";;g)cat "$r";;a)e $r>>$K;;d)cat $K
f a "$r";;l)ls|grep "$r"|sed "s/^/$H /";;esac;;esac


Source: uP2P, mentioned here

six line peer-to-peer client/server in ruby


slonik AZ wrote:
> slashdot published an article on someone's
> 15 lines long Peer-2-Peer application
> http://developers.slashdot.org/article.pl?sid=04/12/15/1953227

> Another person followed up with a 9 line equivalent Perl code.

> I wonder what an equivalent Ruby program would look like?

I did this 9.5 hours ago. Compared to the python one it is not
vulnerable to File stealing attacks (a client can request a file
../foobar and ~/foobar from the python server and will get it back
AFAIK) and 6 lines long. It is however vulnerable to the DRb style
.instance_eval exploits. I will fix this shortly, but I might have to
use 7 lines then.


# Server: ruby p2p.rb password server server-uri merge-servers
# Sample: ruby p2p.rb foobar server druby://localhost:1337 druby://foo.bar:1337
# Client: ruby p2p.rb password client server-uri download-pattern
# Sample: ruby p2p.rb foobar client druby://localhost:1337 *.rb
require'drb';F,D,C,P,M,U,*O=File,Class,Dir,*ARGV;def s(p)F.split(p[/[^|].*/])[-1
]end;def c(u);DRbObject.new((),u)end;def x(u)[P,u].hash;end;M=="client"&&c(U).f(
x(U)).each{|n|p,c=x(n),c(n);(c.f(p,O[0],0).map{|f|s f}-D["*"]).each{|f|F.open(f,
"w"){|o|o<<c.f(p,f,1)}}}||(DRb.start_service U,C.new{def f(c,a=[],t=2)c==x(U)&&(
t==0&&D[s(a)]||t==1&&F.read(s(a))||p(a))end;def y()(p(U)+p).each{|u|c(u).f(x(u),
p(U))rescue()};self;end;private;def p(x=[]);O.push(*x).uniq!;O;end}.new.y;sleep) 


Source: p2p.rb (Florian Gross), mentioned here
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS