1#!/bin/sh
2#	$OpenBSD: run.sh,v 1.1 2005/04/08 17:12:49 cloder Exp $
3#	$EOM: run.sh,v 1.6 1999/08/05 15:02:33 niklas Exp $
4
5#
6# Copyright (c) 1998, 1999 Niklas Hallqvist.  All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28
29#
30# This code was written under funding by Ericsson Radio Systems.
31#
32
33# Defaults
34SRCPORT=1500
35DSTPORT=1501
36FIFO=test.fifo
37TIMEOUT=2
38
39NC=${NC:-/usr/bin/nc}
40ISAKMPD=${ISAKMPD:-/sbin/isakmpd}
41
42progname=`basename $0`
43indent=`echo -n $progname |sed 's/./ /g'`
44seed=980801
45initiator=yes
46retval=0
47verbose=no
48clean=yes
49
50usage ()
51{
52  echo "usage: $progname [-nrv] [-d dst-port] [-f fifo] [-s src-port]" >&2
53  echo "       $indent [-t timeout] testsuite" >&2
54  exit 2
55}
56
57set -- `getopt d:f:nrs:t:v $*`
58if [ $? != 0 ]; then
59  usage
60fi
61for i; do
62  case "$i" in
63  -d)
64    DSTPORT=$2; shift; shift;;
65  -f)
66    FIFO=$2; shift; shift;;
67  -n)
68    clean=no; shift;;
69  -r)
70    initiator=no; shift;;
71  -s)
72    SRCPORT=$2; shift; shift;;
73  -t)
74    TIMEOUT=$2; shift; shift;;
75  -v)
76    verbose=yes; shift;;
77  --)
78    shift; break;;
79  esac
80done
81
82if [ $# -eq 1 ]; then
83  suite=$1
84else
85  usage
86fi
87
88[ ${verbose} = yes ] && set -x
89
90# Start isakmpd and wait for the fifo to get created
91echo Removing ${FIFO}
92rm -f ${FIFO}
93${ISAKMPD} -d -p${SRCPORT} -f${FIFO} -r${seed} &
94isakmpd_pid=$!
95echo isakmpd pid is $isakmpd_pid
96trap 'echo Got signal; kill $isakmpd_pid; echo Removing ${FIFO}; rm -f ${FIFO}; [ $clean = yes ] && rm -f packet' 1 2 15
97while [ ! -p ${FIFO} ]; do
98  sleep 1
99done
100
101# Start the exchange
102if [ $initiator = yes ]; then
103  ${NC} -nul -w${TIMEOUT} 127.0.0.1 ${DSTPORT} </dev/null >packet &
104#  ${NC} -nu -w${TIMEOUT} -p${SRCPORT} 127.0.0.1 ${DSTPORT} </dev/null >packet
105  sleep 1
106  echo "c udp 127.0.0.1:${DSTPORT} 2 1" >${FIFO}
107  in_packets=`ls ${suite}-i.* 2>/dev/null`
108  out_packets=`ls ${suite}-r.* 2>/dev/null`
109else
110  in_packets=`ls ${suite}-r.* 2>/dev/null`
111  out_packets=`ls ${suite}-i.* 2>/dev/null`
112fi
113his_turn=$initiator
114while [ \( $his_turn = yes -a X"$in_packets" != X \) \
115        -o \( $his_turn = no -a X"$out_packets" != X \) ]; do
116  if [ $his_turn = no ]; then
117    set $out_packets
118    packet=$1
119    shift
120    out_packets=$*
121    cat $packet |${NC} -nu -w${TIMEOUT} -p${SRCPORT} 127.0.0.1 ${DSTPORT} \
122      >packet
123    my_turn=no
124  else
125    set $in_packets
126    packet=$1
127    shift
128    in_packets=$*
129    if ! cmp $packet packet 2>/dev/null; then
130      retval=1
131      break
132    fi
133    my_turn=yes
134  fi
135done
136kill $isakmpd_pid
137rm -f ${FIFO}
138[ $clean = yes ] && rm -f packet
139exit $retval
140