* ext/openssl/ossl_ssl.c: sync_close is moved to SSLSocket as

a builtin.

* ext/openssl/lib/openssl/buffering.rb (Buffering#close): ditto.

* ext/openssl/lib/openssl/buffering.rb (Buffering#puts): should
  add a return to the tails of each line.

* ext/openssl/lib/openssl/ssl.rb: new class OpenSSL::SSL::SSLServer.

* ext/openssl/lib/net/protocols.rb (SSLIO#ssl_connect): use sync_close.

* ext/openssl/sample/echo_svr.rb: use SSLServer.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4407 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
gotoyuzo 2003-08-18 22:49:48 +00:00
parent 40fc7dba9e
commit ba64282cdc
7 changed files with 95 additions and 38 deletions

View File

@ -1,3 +1,21 @@
Tue Aug 19 07:47:09 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
* ext/openssl/ossl_ssl.c: sync_close is moved to SSLSocket as
a builtin.
* ext/openssl/lib/openssl/buffering.rb (Buffering#close): ditto.
* ext/openssl/lib/openssl/buffering.rb (Buffering#puts): should
add a return to the tails of each line.
* ext/openssl/lib/openssl/ssl.rb: new class OpenSSL::SSL::SSLServer.
* ext/openssl/lib/net/protocols.rb (SSLIO#ssl_connect): use sync_close.
* ext/openssl/sample/echo_svr.rb: use SSLServer.
* ext/openssl/sample/echo_cli.rb: add example of SSLSocket#sync_close.
Tue Aug 19 01:24:34 2003 Nobuyoshi Nakada <nobu@ruby-lang.org> Tue Aug 19 01:24:34 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/curses/curses.c (_XOPEN_SOURCE_EXTENDED): Mac OS X standard * ext/curses/curses.c (_XOPEN_SOURCE_EXTENDED): Mac OS X standard

View File

@ -40,17 +40,12 @@ module Net
end end
def ssl_connect() def ssl_connect()
@raw_socket = @socket @socket = OpenSSL::SSL::SSLSocket.new(@socket, @ssl_context)
@socket = OpenSSL::SSL::SSLSocket.new(@raw_socket, @ssl_context) @socket.sync = true
@scoket.sync = true @socket.sync_close = true
@socket.connect @socket.connect
end end
def close
super
@raw_socket.close if @raw_socket
end
def peer_cert def peer_cert
@socket.peer_cert @socket.peer_cert
end end

View File

@ -16,7 +16,7 @@
module Buffering module Buffering
include Enumerable include Enumerable
attr_accessor :sync, :sync_close attr_accessor :sync
BLOCK_SIZE = 1024*16 BLOCK_SIZE = 1024*16
# #
@ -158,7 +158,12 @@ module Buffering
def puts(*args) def puts(*args)
s = "" s = ""
args.each{ |arg| s << arg.to_s + $/ } args.each{|arg|
s << arg.to_s
unless /#{$/}\Z/o =~ s
s << $/
end
}
do_write(s) do_write(s)
nil nil
end end
@ -183,9 +188,7 @@ module Buffering
end end
def close def close
flush flush rescue nil
sysclose sysclose
@sync_close ||= false
@io.close if @sync_close
end end
end end

View File

@ -18,31 +18,66 @@ require 'openssl/buffering'
module OpenSSL module OpenSSL
module SSL module SSL
class SSLSocket module SocketForwarder
include Buffering
def addr def addr
@io.addr to_io.addr
end end
def peeraddr def peeraddr
@io.peeraddr to_io.peeraddr
end end
def getsockopt(level, optname, optval) def getsockopt(level, optname, optval)
@io.setsockopt(level, optname, optval) to_io.setsockopt(level, optname, optval)
end end
def setsockopt(level, optname) def setsockopt(level, optname)
@io.setsockopt(level, optname) to_io.setsockopt(level, optname)
end end
def fcntl(*args) def fcntl(*args)
@io.fcntl(*args) to_io.fcntl(*args)
end end
def closed? def closed?
@io.closed? to_io.closed?
end
end
class SSLSocket
include Buffering
include SocketForwarder
end
class SSLServer
include SocketForwarder
attr_accessor :start_immediately
def initialize(svr, ctx)
@svr = svr
@ctx = ctx
@start_immediately = true
end
def to_io
@svr
end
def listen(basklog=5)
@svr.listen(backlog)
end
def accept
sock = @svr.accept
ssl = OpenSSL::SSL::SSLSocket.new(sock, @ctx)
ssl.sync = true
ssl.sync_close = true
ssl.accept if @start_immediately
ssl
end
def close
@svr.close
end end
end end
end end

View File

@ -335,11 +335,14 @@ ossl_sslctx_set_ciphers(VALUE self, VALUE v)
*/ */
#define ossl_ssl_get_io(o) rb_iv_get((o),"@io") #define ossl_ssl_get_io(o) rb_iv_get((o),"@io")
#define ossl_ssl_get_ctx(o) rb_iv_get((o),"@context") #define ossl_ssl_get_ctx(o) rb_iv_get((o),"@context")
#define ossl_ssl_get_sync_close(o) rb_iv_get((o),"@sync_close")
#define ossl_ssl_set_io(o,v) rb_iv_set((o),"@io",(v)) #define ossl_ssl_set_io(o,v) rb_iv_set((o),"@io",(v))
#define ossl_ssl_set_ctx(o,v) rb_iv_set((o),"@context",(v)) #define ossl_ssl_set_ctx(o,v) rb_iv_set((o),"@context",(v))
#define ossl_ssl_set_sync_close(o,v) rb_iv_set((o),"@sync_close",(v))
static char *ossl_ssl_attrs[] = { "io", "context", }; static char *ossl_ssl_attr_readers[] = { "io", "context", };
static char *ossl_ssl_attrs[] = { "sync_close", };
static void static void
ossl_ssl_shutdown(SSL *ssl) ossl_ssl_shutdown(SSL *ssl)
@ -376,6 +379,7 @@ ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
Check_Type(io, T_FILE); Check_Type(io, T_FILE);
ossl_ssl_set_io(self, io); ossl_ssl_set_io(self, io);
ossl_ssl_set_ctx(self, ctx); ossl_ssl_set_ctx(self, ctx);
ossl_ssl_set_sync_close(self, Qfalse);
ossl_sslctx_setup(ctx); ossl_sslctx_setup(ctx);
return self; return self;
@ -522,8 +526,9 @@ ossl_ssl_close(VALUE self)
SSL *ssl; SSL *ssl;
Data_Get_Struct(self, SSL, ssl); Data_Get_Struct(self, SSL, ssl);
ossl_ssl_shutdown(ssl); ossl_ssl_shutdown(ssl);
if (RTEST(ossl_ssl_get_sync_close(self)))
rb_funcall(ossl_ssl_get_io(self), rb_intern("close"), 0);
return Qnil; return Qnil;
} }
@ -635,8 +640,10 @@ Init_ossl_ssl()
/* class SSLSocket */ /* class SSLSocket */
cSSLSocket = rb_define_class_under(mSSL, "SSLSocket", rb_cObject); cSSLSocket = rb_define_class_under(mSSL, "SSLSocket", rb_cObject);
rb_define_alloc_func(cSSLSocket, ossl_ssl_s_alloc); rb_define_alloc_func(cSSLSocket, ossl_ssl_s_alloc);
for(i = 0; i < numberof(ossl_ssl_attr_readers); i++)
rb_attr(cSSLSocket, rb_intern(ossl_ssl_attr_readers[i]), 1, 0, Qfalse);
for(i = 0; i < numberof(ossl_ssl_attrs); i++) for(i = 0; i < numberof(ossl_ssl_attrs); i++)
rb_attr(cSSLSocket, rb_intern(ossl_ssl_attrs[i]), 1, 0, Qfalse); rb_attr(cSSLSocket, rb_intern(ossl_ssl_attrs[i]), 1, 1, Qfalse);
rb_define_alias(cSSLSocket, "to_io", "io"); rb_define_alias(cSSLSocket, "to_io", "io");
rb_define_method(cSSLSocket, "initialize", ossl_ssl_initialize, -1); rb_define_method(cSSLSocket, "initialize", ossl_ssl_initialize, -1);
rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0); rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0);

View File

@ -26,11 +26,12 @@ end
s = TCPSocket.new(host, port) s = TCPSocket.new(host, port)
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx) ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
ssl.connect ssl.connect # start SSL session
ssl.sync_close = true # if true the underlying socket will be
# closed in SSLSocket#close. (default: false)
while line = $stdin.gets while line = $stdin.gets
ssl.write line ssl.write line
print ssl.gets print ssl.gets
end end
ssl.close ssl.close
s.close

View File

@ -51,14 +51,12 @@ else
$stderr.puts "!!! WARNING: PEER CERTIFICATE WON'T BE VERIFIED !!!" $stderr.puts "!!! WARNING: PEER CERTIFICATE WON'T BE VERIFIED !!!"
end end
svr = TCPServer.new(port) tcps = TCPServer.new(port)
ssls = OpenSSL::SSL::SSLServer.new(tcps, ctx)
loop do loop do
ns = svr.accept ns = ssls.accept
ssl = OpenSSL::SSL::SSLSocket.new(ns, ctx) while line = ns.gets
ssl.accept ns.write line
while line = ssl.gets
ssl.write line
end end
ssl.close
ns.close ns.close
end end