#!/bin/ksh #This shellscript is tcp_receiver. #Split the standard input into separate files for each port number. #The names of the files are /tmp/$$.$port #Then feed each file to the correct program in the directory given as a #command line argument. #Sample use: tcp_receiver 0_receiverhost if [[ $# -ne 1 ]] then echo $0: needs one directory name as a command line argument 1>&2 exit 1 fi cat > /tmp/$$ while [[ -s /tmp/$$ ]] do #Read the three-byte TCP header. length=`chopoff 1 /tmp/$$` port=`chopoff 1 /tmp/$$` sequence_number=`chopoff 1 /tmp/$$` #Demultiplex: write the TCP segment to the file for that port number. echo $length'\c' >> /tmp/$$.$port echo $port'\c' >> /tmp/$$.$port echo $sequence_number'\c' >> /tmp/$$.$port chopoff $((length - 3)) /tmp/$$ | tr '\012' X >> /tmp/$$.$port echo >> /tmp/$$.$port done rm /tmp/$$ for prog in $1/* do port=`echo ${prog##*/} | sed 's/\(.\).*/\1/'` if [[ -s /tmp/$$.$port ]] then sort +0.1n -0.2 /tmp/$$.$port | tr -d '\012' < /tmp/$$.$port | tr X '\012' > /tmp/$$.$port.sorted rm /tmp/$$.$port while [[ -s /tmp/$$.$port.sorted ]] do length=`chopoff 1 /tmp/$$.$port.sorted` chopoff 1 /tmp/$$.$port.sorted > /dev/null #Discard prort number. chopoff 1 /tmp/$$.$port.sorted > /dev/null #Discard sequence number. chopoff $((length - 3)) /tmp/$$.$port.sorted | $prog done rm /tmp/$$.$port.sorted fi done exit 0