10
0
Fork 0
softsim/src/apdu_client.rb

41 lines
979 B
Ruby
Executable File

#!/usr/bin/env ruby
# This programm will forward APDU from a TCP port to a SAP server
require 'lib/client'
require 'socket'
SAP_HOST = "localhost"
SAP_PORT = "1337"
APDU_SOCKET = "/tmp/apdu.socket"
# wait for a client to connect
socket = UNIXServer.new(APDU_SOCKET)
io = socket.accept
# create SAP client to SAP server
sap = TCPSocket.open(SAP_HOST,SAP_PORT)
client = Client.new(sap,0)
client.start
client.connect
atr = client.atr
puts atr ? "ATR : #{atr.collect{|x| x.to_s(16).rjust(2,'0')}*' '}" : "could not get ATR"
@end = false
until @end do
activity = IO.select([io])
begin
input = activity[0][0].readpartial(0xffff)
req = input.unpack("C*")
puts "> #{req.collect{|x| x.to_s(16).rjust(2,'0')}*' '}"
resp = client.apdu(req)
puts "< #{resp.collect{|x| x.to_s(16).rjust(2,'0')}*' '}"
io.write resp.pack("C*")
rescue EOFError,Errno::EPIPE
$stderr.puts "source disconnected"
@end = true
end
end
io.close
client.disconnect
sap.close